Android Four Components Learning Activity VI

Source: Internet
Author: User

This section learns the state of activity preservation and recovery.

Let's start with an example:

Layout files are mainly implemented as follows, you write your own


Activity Logic Code:

public class Fiveactivity extends Activity {private button setbutton;private button getbutton;private EditText edittext;p rivate float value; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.activity_five); Setbutton = (Button) findViewById ( R.id.button1); Getbutton = (Button) Findviewbyid (r.id.button2); editText = (EditText) Findviewbyid (R.ID.EDITTEXT1); Setbutton.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stub value = 1.234f;edittext.settext ("Hello World");}); Getbutton.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubstring string = Edittext.gettext (). toString (); Toast.maketext (Fiveactivity.this, string+ "" +value, 0). Show ();}}); @Overrideprotected void Onsaveinstancestate (Bundle outstate) {//TODO auto-generated method stublog.i ("Fiveactivity", " OnsaveinstancesTate "); Super.onsaveinstancestate (outstate);}} 
First case:
Press the Set button, and press the Get button


Of course, when you press the Back button, when you re-enter, you will find that there is no content in the input box, and press get to get the 0.0


At the same time in the first case my print log.i ("fiveactivity", "onsaveinstancestate");

Then the second scenario:

After we set, press get again. This time do not press the back key, this time press the home key:

When we press the home key, our activity is in the stop state, when our print statement appears


This time we kill fiveactivity manually.


When you start the activity, you will find it when you get it again:


The text in EditText Hello World still exists, and my value variable becomes 0.0.


Summarize:

1: For the first case, the Android system feels that the user is volunteering, that there is no need to save.

2: For the second case, the text in the EditText exists because the Onsaveinstancestate method is implemented in almost all of the UI in the Android system, which is the default implementation. So when we go out, we print out, and as for why our variables aren't recovered, it's obvious that no one is saving the value, so it needs to be saved manually by our programmers.


When we add in the OnCreate method:

if (savedinstancestate! = null) value = Savedinstancestate.getfloat ("1234");

protected void Onsaveinstancestate (Bundle outstate) {//TODO auto-generated method stublog.i ("Fiveactivity", " Onsaveinstancestate "); Outstate.putfloat (" 1234 ", value); Super.onsaveinstancestate (outstate);}

In this case, when the kill process comes in again, our data will appear normally.


At the same time, in addition to the lack of system resources, or the phone's screen orientation changes, changes in language, will cause the activity to reproduce the destruction, and then re-create. This is not the case if we don't save the data we need to save.







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Four Components Learning Activity VI

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.