Android Learning Summary activity save and restore status _android

Source: Internet
Author: User

Start an activity in Android if you click the Home button, the activity is not destroyed, but some data is lost when you do something, as follows:

Java class:

Package com.king.activitytest2;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.Toast; public class Mainactivity extends appcompatactivity implements view.onclicklistener{private EditText edittext; private B
Utton Buttonset,buttonread;
Defines a constant double pai; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
Gets the control edittext= (edittext) Findviewbyid (R.ID.EDIT1);
buttonset= (Button) Findviewbyid (R.id.btn_set);
Buttonread= (Button) Findviewbyid (R.id.btn_read);
Set up the Listener event Buttonset.setonclicklistener (this);
Buttonread.setonclicklistener (this); @Override public void OnClick (view view) {switch (View.getid ()) {case R.id.btn_set:edittext.settext ("PI is:"); pai=3.14
1592654;
Break
Case R.id.btn_read:string Str=edittext.gettext (). toString () +pai; Toast.maketext (mainactivity.this, Str,toast.length_short). Show ();
Break }
}
}

XML layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" tools:context= "com.king.activitytest2.MainActivity" > <edittext android:layout_width= "Match_" Parent "android:layout_height=" wrap_content "android:id=" @+id/edit1 "/> <button android:layout_width=" Wrap_ Content "android:layout_height=" wrap_content "android:text=" Settings "android:id=" @+id/btn_set "android:layout_margintop = "46DP" android:layout_below= "@+id/edit1" android:layout_alignparentleft= "true" android:layout_alignparentstart= " True "android:layout_marginleft=" 35DP "android:layout_marginstart=" 35DP "/> <button android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:text= read" android:id= "@+id/btn_read" Android:layout_ aligntop= "@+id/btn_set" android:layout_alignparentright= "true" android:layout_alignparentend= "TruE "android:layout_marginright=" 46DP "android:layout_marginend=" 46DP "/> </RelativeLayout> 

Here in the program defines a variable double, when we click on the Read button will be the value of the variable added to the back and display, open the program and then operate, all normal:

We clicked on the settings and then the screen came across, hey, where's pai?

In fact, when we are in the same screen switching, under the system mechanism, the activity has been destroyed. But why this page still exists, this is because the destruction is not the user to voluntarily quit, so the Android system to achieve these state of the Save function, but some data can not be retained requirements, and not be retained, like the program in poor Pai was abandoned!

So what do we need to do to preserve the data in a switch like this one, where we need to rewrite the Onsaveinstancestate () method:

@Override
protected void onsaveinstancestate (Bundle outstate) {
super.onsaveinstancestate (outstate);
Place the data that needs to be saved into the bundle
outstate.putdouble ("Pai", pai);
}

Save the solution, how do we take it out? Simply, you need to determine whether the parameter is null in the OnCreate () method, and it is not null to remove it.

@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
Save the Pai
if (savedinstancestate!=null)
pai=savedinstancestate.getdouble ("pai");
}

Now we can see that the PAI has not been destroyed after the screen switch.

The above is a small set to introduce the activity to save and restore status, I hope to help everyone, if you have any questions please give me a message, small set will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.