java - Android - Turning on "Don't keep activities" in Android Developer options duplicates my FragmentActivity -
i getting error few users of application when clicking listitem mainactivity take them detailactivity (which desired outcome), if press or up, see mainactivity had duplicated (listview, menuitems). suggested maybe activity being killed because of memory went developer options on phone , turned on "don't keep activities". allowed me replicate problem. have figure out why duplicated, i'm little stuck direction go into. it's not i'm creating menu , layout in onresume(). i'm doing in oncreate(). have suggestions on gracefully recovering activity not kept?
edit 1:
in response comment @tomer mor
i have 3 activities total. -> b-> c
mainactivity in case b , detailactivity c. activity "loginactivity" get's finish()'d after "login".
edit 2:
if helps, mainactivity fragmentactivity.
edit 3:
in response @doctoror drive. mainactivity extends fragmentactivity. mainactivity adds myfragment.java
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setupactionbar(); toast.maketext(this, this.getclass().getsimplename(), 1).show(); setcontentview(r.layout.my_placement); myfragment = new myfragment(); fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.add(r.id.fragment_container, myfragment); fragmenttransaction.commit(); }
i put log inside of oncreate of mainactivity extends fragmentactivity
, get's called once. put log.d()
in oncreateoptionsmenu()
of of myfragment.java
, shows twice! being called twice, i'm not sure how if it's being created once.
edit 4:
03-24 14:17:37.038: e/mainactivity(26486): oncreate hit! 03-24 14:17:37.048: d/sherlockfragmentactivity(26486): [supportinvalidateoptionsmenu] 03-24 14:17:37.048: d/sherlockfragmentactivity(26486): [invalidateoptionsmenu] 03-24 14:17:37.048: d/actionbarsherlock(26486): [dispatchinvalidateoptionsmenu] 03-24 14:17:37.048: e/myfragment(26486): onactivitycreated! 03-24 14:17:37.048: d/sherlockfragmentactivity(26486): [supportinvalidateoptionsmenu] 03-24 14:17:37.048: d/sherlockfragmentactivity(26486): [invalidateoptionsmenu] 03-24 14:17:37.048: d/actionbarsherlock(26486): [dispatchinvalidateoptionsmenu] 03-24 14:17:37.048: e/myfragment(26486): onactivitycreated!
edit 5:
@override public void onactivitycreated(bundle savedinstancestate) { savedinstancestate=null; super.onactivitycreated(savedinstancestate); sethasoptionsmenu(true); adapter = animallist.getanimaladapter(); setlistadapter(adapter); }
adding fragment while activity restores it's state common bug. should add fragment when activity's savedinstancestate s null, otherwise fragmentmanager adds automatically , end 2 fragment instanced being attached.
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); ... if (savedinstancestate == null) { final fragmentmanager fragmentmanager = getsupportfragmentmanager(); final fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.add(r.id.fragment_container, myfragment); fragmenttransaction.commit(); } }
and other bad practice using members access fragments. fragment's lifecycle can little different activity, myfragment member can point obsolete fragment instance @ point. proper way whenever need myfragment, find in fragmentmanager
//if need myfragment public void dosomething() { final myfragment myfragment = getmyfragment(); // can null if re-creating or not yet attached. if (myfragment != null) { myfragment.dosomething(); } } private myfragment getmyfragment() { return (myfragment) getsupportfragmentmanager().findfragmentbyid(r.id.fragment_container); }
Comments
Post a Comment