android - Pass data to Fragment from Layout before it's rendered -
i need pass data fragment, declared in activity layout before rendered:
<fragment android:name="com.app.fragments.myfragment" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/myfragment" />
i tried in activity's oncreate
fragment rendered @ time.
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // here fragment rendered mposts = (myfragment)getsupportfragmentmanager().findfragmentbyid(r.id.myfragment); mposts.setdata(somedata); }
i've seen way when fragment created programmatically in activity oncreate
, added container. it's not bad solution. but... there simpler?
for xml layout there 1 question on here: https://stackoverflow.com/a/23226281/842607
even has cannot pass arguments, can use callbacks or custom attributes.
else
normally, use bundle
in arguments in way:
fragment class
public class shopproductlistingfragment extends fragment { private static final string tag = shopproductlistingfragment.class.getsimplename(); public static final string key_category = "category"; public static final string key_url_params = "url_params"; public static final string key_url = "url"; public static shopproductlistingfragment getinstance(newcategory category, @nonnull hashmap<string, string> urlparams) { bundle bundle = new bundle(); if (null != category) bundle.putlong(key_category, category.getcategory_id()); bundle.putserializable(key_url_params, urlparams); shopproductlistingfragment fragment = new shopproductlistingfragment(); fragment.setarguments(bundle); return fragment; } public static shopproductlistingfragment getinstance(@nonnull string url) { bundle bundle = new bundle(); bundle.putserializable(key_url, url); shopproductlistingfragment fragment = new shopproductlistingfragment(); fragment.setarguments(bundle); return fragment; } public shopproductlistingfragment() { // required empty public constructor } @override public void oncreate(@nullable bundle savedinstancestate) { super.oncreate(savedinstancestate); initvals(); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_shop_product_listing, container, false); butterknife.bind(this, rootview); initviews(); return rootview; } private void initvals() { bundle bundle = getarguments(); if (null == bundle || bundle.empty == bundle) throw new nullpointerexception("invalid or null arguments passed fragment shopproductlistingfragment"); if (bundle.containskey(key_category)) categoryid = bundle.getlong(key_category); mcategory = styfi.getinstance().getcategory(categoryid); if (bundle.containskey(key_url_params)) urlparams = (hashmap<string, string>) bundle.getserializable(key_url_params); if (bundle.containskey(key_url)) urlfilter = bundle.getstring(key_url); } }
layout file of activity
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <framelayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> </linearlayout>
this how instantiated in activity
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity); if (null == savedinstancestate) { shopproductlistingfragment fragment = shopproductlistingfragment.getinstance(category, geturlparams(data)); fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction().replace(r.id.content_frame, fragment, "shop"); fragmenttransaction.commit(); } }
Comments
Post a Comment