android - Why is my custom object not persisted by onSaveInstanceState and onRestoreInstanceState -


when activity gets called first time, called in intent. received , stored in data member:

class editblindscheduleactivity extends activity {   private blindschedule blindschedule;    protected void oncreate(bundle savedinstancestate)    {     ...      if (savedinstancestate == null) {  // not recreating, first load.       blindschedule = (blindschedule) getintent().getserializableextra("blindschedule");     } 

there simple if/else determine if have blindschedule object or not.

if (blindschedule == null) {   settitle("create blind schedule"); } else {   settitle("edit blind schedule"); } 

when load activity first time, indeed, title "edit blind schedule", meaning there blindschedule object.

unfortunately, when rotate screen twice title reads "create blind schedule", meaning blindschedule object null , has failed persisted.

why blindschedule object not being persisted, , can it?

full code follows:

public class editblindscheduleactivity extends activity {    private blindschedule blindschedule;    boolean creating;  // creating or updating?    @override   protected void oncreate(bundle savedinstancestate)    {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_edit_blind_schedule);      if (savedinstancestate == null) {  // not recreating, first load.       blindschedule = (blindschedule) getintent().getserializableextra("blindschedule");     }      if (blindschedule == null) {       creating = true;       settitle("create blind schedule");     } else {       creating = false;       settitle("edit blind schedule");     }   }    @override   protected void onrestoreinstancestate(bundle savedinstancestate) {     super.onrestoreinstancestate(savedinstancestate);     blindschedule = (blindschedule) savedinstancestate.getserializable("blindschedule");   }    @override   protected void onsaveinstancestate(bundle outstate) {     super.onsaveinstancestate(outstate);     outstate.putserializable("blindschedule", blindschedule);   } 

you need retrieve saved custom object in oncreate in case.

according official documentation

onrestoreinstancestate called after onstart time settitle has been called.

add else part if (savedinstancestate == null) , retrieve blindschedule in there same way getintent

if (savedinstancestate == null)  {  // not recreating, first load.   blindschedule = (blindschedule) getintent().getserializableextra("blindschedule"); } else {   blindschedule = (blindschedule) savedinstancestate.getserializableextra("blindschedule"); } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -