i have fragment view , options menu:
public class offeringsfragment extends fragment { public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { sethasoptionsmenu(true); view view = inflater.inflate(r.layout.offering_tiles, null); ... } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_settings) { intent intent = new intent(getactivity(), settingsactivity.class); startactivity(intent); return true; } return super.onoptionsitemselected(item); } }
from options menu, user opens preference fragment, hosted settingsactivity:
public class settingsactivity extends activity { private settingsfragment settingsfragment = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); settingsfragment = new settingsfragment(); getfragmentmanager().begintransaction() .replace(android.r.id.content, settingsfragment) .commit(); }
the view of offeringsfragment depends on 1 of preferences. is, after preference has changed, offeringsfragment must refreshed calling oncreateview again. this:
- open preference screen offeringsfragment's option menu
- change preference
- return offeringsfragment
if return offeringsfragment via home button (left arrow in actionbar), offeringsfragment gets refreshed calling oncreateview (which desired effect). however, if return offeringsfragment via button (on device), oncreateview not called , view not re-created. want view re-created when user presses button. ideas how achieve this?
what happens
when press button parent activity called via startactivity
means new instance created default.
when press button current activity finished , you're in previous activity , existing instance (it in stopped state).
how deal it
what want view re-created when user presses button. ideas how achieve this?
start settings activity via startactivityforresult
:
public static final int rc_settings = 1; private void startsettingsactivity() { intent = new intent(this, settingsactivity.class); startactivityforresult(i, rc_settings); }
when result comes reattach fragment. recreate view hierarchy.
@override public void onactivityresult(int requestcode, int resultcode, intent data) { // call super if value life. , want proper lifecycle handling. super.onactivityresult(requestcode, resultcode, data); if (requestcode == rc_settings) { // if came settingsactivity... // ...reattach fragment , trigger view recreation. final fragmentmanager fm = getfragmentmanager(); final f = fm.findfragmentbyid(android.r.id.content); fm.begintransaction().detach(f).attach(f).commit(); } }
replace fragment id whatever used.
pro tip
if fragment not misconfigured should able call
public class settingsactivity extends activity { private settingsfragment settingsfragment = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (savedinstancestate == null) { settingsfragment = new settingsfragment(); getfragmentmanager().begintransaction() .replace(android.r.id.content, settingsfragment) .commit(); } else { settingsfragment = (settingsfragment) getfragmentmanager().findfragmentbyid(android.r.id.content); } } }
this both resourceful , practical original code lose state (for example scroll position) on configuration change.
Comments
Post a Comment