In the development process of Android, it is often necessary to save the previous data. This is the time to do the protection scene operation, the operation is mainly to rewrite the onsaveinstancestate and onrestoreinstancestate two functions.
Onsaveinstancestate, saving data
The order of execution of the activity life cycle:
Onpause-onsaveinstancestate-onstop-ondestroy
Onrestoreinstancestate, recovering data
The order of execution of the activity life cycle:
Onstart-onrestoreinstancestate-onresume
The following example protects the data of the background color with the cell phone screen as an example.
Packagecom.example.hjw.androidday0602;ImportAndroid.graphics.Color;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;Importandroid.widget.LinearLayout;ImportAndroid.widget.Toast;ImportJava.util.Random; Public classMainactivityextendsappcompatactivity {PrivateLinearLayout LinearLayout; Privatebutton button; String R,g,b,c; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); LinearLayout=(LinearLayout) Findviewbyid (R.ID.L1); Button=(Button) Findviewbyid (R.ID.B1); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {random random=NewRandom (); R= Integer.tohexstring (Random.nextint (256) . toUpperCase (); G= Integer.tohexstring (Random.nextint (256) . toUpperCase (); b= Integer.tohexstring (Random.nextint (256) . toUpperCase (); R= R.length () ==1? "0" +r:r; G= G.length () ==1? "0" +g:g; b= B.length () ==1? "0" +b:b; C= "#" +r+b+G; Toast.maketext (mainactivity. This, C,toast.length_short). Show (); Linearlayout.setbackgroundcolor (Color.parsecolor (c)); } }); } //Save Data//parameter is bundle type, used to store data@Overrideprotected voidonsaveinstancestate (Bundle outstate) {//TODO auto-generated Method Stub Super. Onsaveinstancestate (outstate); if(c!=NULL) {outstate.putstring ("Color", c); } } //Recovering Data//parameter is bundle type, used to read data@Overrideprotected voidonrestoreinstancestate (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. Onrestoreinstancestate (savedinstancestate); C= savedinstancestate.getstring ("Color"); Linearlayout.setbackgroundcolor (Color.parsecolor (c)); } //re-restore the value@Overrideprotected voidOnresume () {//TODO auto-generated Method Stub Super. Onresume (); Toast.maketext (mainactivity. This, C,toast.length_short). Show (); }}
Front Code
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/l1"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.hjw.androidday0602.MainActivity"> <ButtonAndroid:id= "@+id/b1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Jump"/></LinearLayout>
Operating effect:
Android Protection Site Example