android - Loader is not retained after starting new activity, changing orientation in new activity and pressing back button -
i been running issue loaders lately. created small project reproduces issue https://github.com/solcott/loaders-orientation-change
i have simple activity adds fragment
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); fragment mainfragment = getfragmentmanager().findfragmentbyid(r.id.main_fragment); if(mainfragment == null){ log.w(getclass().getsimplename(), "creating new fragment"); mainfragment = new mainfragment(); getfragmentmanager().begintransaction().add(r.id.main_fragment, mainfragment).commit(); } } }
in fragment start loader returns integer displayed on screen. there button starts new activity:
public class mainfragment extends fragment implements loadercallbacks<integer> { private static int next_val = 0; textview text1; button button1; @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); getloadermanager().initloader(1, null, this); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.fragment_main, container, false); } @override public void onviewcreated(view view, bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); text1 = (textview) view.findviewbyid(android.r.id.text1); button1 = (button) view.findviewbyid(android.r.id.button1); button1.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { startactivity(new intent(getactivity(), nextactivity.class)); } }); } @override public loader<integer> oncreateloader(int id, bundle args) { asynctaskloader<integer> loader = new asynctaskloader<integer>( getactivity()) { @override public integer loadinbackground() { return next_val++; } }; loader.forceload(); return loader; } @override public void onloadfinished(loader<integer> loader, integer data) { text1.settext(data.tostring()); } @override public void onloaderreset(loader<integer> loader) { // todo auto-generated method stub } }
up point works fine can change orientation , integer displayed doesn't change. can start new activity , hit button , integer displayed doesn't change.
however, when navigate new activity, change orientation , press button integer incremented. expect behave same way did without orientation change.
calling setretaininstance(true) in fragment.oncreate() make worse. onloadcomplete never called , integer not displayed @ all.
has else run issue , found workaround?
same question asked here: loader unable retain during configuration change
this damn annoying problem asynctask loaders. although solution proposed in thread, think general consensus avoid asynctaskloaders.
Comments
Post a Comment