Android Protection Site Example

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.