android - How to keep fragments in memory on orientation change -
i'm creating tablet optimised application using fragments. have thin navigation fragment down left hand side buttons controls fragments loaded 2 viewgroup containers take rest of screen.
for example, if button in navigation fragment pressed, 1st container loaded item list fragment (eg settings, customers etc) , second container loaded fragment show item details.
since want retain fragments in memory (so, when navigating settings customers , settings, previous settings fragments should there user doesn't lose to) - have created fragments instance variables activity
settingslistfragment settingslistfragment; settingsdetailfragment settingsdetailfragment; customerlistfragment customerlistfragment; customerdetailfragment customerdetailfragment;
i create these fragments when neccessary , replace containers fragments needed. eg.
fragmentmanager fragmentmanager = getfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); if(settingslistfragment == null){ settingslistfragment = new settingslistfragment(); } fragmenttransaction.replace(r.id.fragment_container_left, settingslistfragment); fragmenttransaction.commit();
my question is, how can retain these instances accross orientation change?
i'm assuming shouldn't use activities onretainnonconfigurationinstance() method according http://developer.android.com/resources/articles/faster-screen-orientation-change.html because "be careful object pass through onretainnonconfigurationchange() ...this means should never pass view, drawable, adapter, etc"
i've attempted fragment fragment manager, fragment clf = fragmentmanager.findfragmentbytag("client list");
comes null. think gets released after fragment replaced.
thanks help/advice.
you can't (or shouldn't) retain fragments in way. i'm guessing it's state want keep.
if store state in onsaveinstancestate()
can later retrive in oncreate()
or onactivitycreate()
. after go (you'll have add fragmenttransaction
stack) can restore state bundle saved it!
fragmentmanager
take care of handling configuration changes , restoring fragments in current views.
Comments
Post a Comment