android - How to pass data from activity to it's own fragment? -
i want pass data activity fragment manages. have been trying code below: activity code:
string accountnumber = getintent().getstringextra("accountnumber").tostring(); fragment fragment = new fragment(); bundle bundle = new bundle(); bundle.putstring("accountnumber", accountnumber); fragment.setarguments(bundle);
fragment code inside oncreateview method:
bundle args = getarguments(); if (args != null && args.containskey("accountnumber")) { accountnumber = args.getstring("accountnumber").tostring(); toast toast = toast.maketext(getactivity(), accountnumber, toast.length_short); toast.show(); }
the string accountnumber intent.getextra not null, when pass fragment accountnumber results null ...
i tried fragmentmanager in activity class this:
public void oncreate(bundle savedinstancestate) { // ... if (savedinstancestate == null) { fragmentmanager fragmentmanager = getfragmentmanager() // or: fragmentmanager fragmentmanager = getsupportfragmentmanager() fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragment fragment = new fragment(); bundle bundle = new bundle(); bundle.putstring("accountnumber", accountnumber); fragment.setarguments(bundle); fragmenttransaction.replace(r.id.fragment_container, fragment); fragmenttransaction.commit(); } }
check kind of "fragment" did import in activity. importing , instantiating "android.app.fragment" while expecting argument magically show in "com.my.application.fragment".
Comments
Post a Comment