1. Save data before device rotation
When the screen rotates, the current activity is destroyed and a new activity is created, which can result in loss of data. The data is saved by overwriting the Onsaveinstancestate (Bundle outstate) method.
@Overrideprotected voidonsaveinstancestate (Bundle outstate) {Super. Onsaveinstancestate (outstate); LOG.I (TAG,"Onsaveinstancestate"); Outstate.putint (Key_index,mcurrentindex); } @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_quiz); if(savedinstancestate!=NULL) {Mcurrentindex=savedinstancestate.getint (key_index,0); } Mquestiontextview=(TextView) Findviewbyid (R.id.question_text); Updatequestion (); MTRUEBTN=(Button) Findviewbyid (R.ID.TRUE_BTN); MFALSEBTN=(Button) Findviewbyid (R.ID.FALSE_BTN); Mnextbutton=(Button) Findviewbyid (R.id.next_button); MPREVBTN=(Button) Findviewbyid (R.id.prev_button); Mtruebtn.setonclicklistener ( This); Mfalsebtn.setonclicklistener ( This); Mnextbutton.setonclicklistener ( This); Mprevbtn.setonclicklistener ( This); }
One thing to note is that the data stored and recovered in bundles can only be basic data types and objects that can implement the serializable interface.
Reading notes--android programming authoritative guide