android - Using onSaveInstanceState with fragments in backstack? -


i have fragments keep in backstack of fragmentmanager. every fragment state saved orientation changes member variables, example:

@override public void onsaveinstancestate(bundle outstate)  {     super.onsaveinstancestate(outstate);     outstate.putlong("userid", muserid);     outstate.putstring("username", musername); } 

my problem if there orientation change, since every fragment in backstack gets called via onsaveinstancestate, null pointer exception because member variables don't exist anymore.

any ideas on how solve this?

it possible member variables don't exist anymore because fragmentmanager in activity dying of fragments.

you need override method onsaveinstancestate of activity class well, because need save activity state before save fragments state.

as documentation says:

there many situations fragment may torn down (such when placed on stack no ui showing), state not saved until owning activity needs save state.

update

in activity onsaveinstancestate , onrestoreinstancestate, try saving fragment references , restore them this:

public void onsaveinstancestate(bundle outstate){     getfragmentmanager().putfragment(outstate,"myfragment",myfragment); } public void onrestoreinstancestate(bundle instate){     myfragment = getfragmentmanager().getfragment(instate,"myfragment"); } 

tell me if had luck! :-)


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -