Activity 6 of four Android components Learning

Source: Internet
Author: User

Activity 6 of four Android components Learning

This section describes how to save and restore the Activity status.

Start with an example:

The layout file is mainly implemented as follows.

Activity logic code:

 

public class FiveActivity extends Activity {private Button setButton;private Button getButton;private EditText editText;private 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 the get button.

 

Of course, press the back button at this time. When you enter the page again, you will find that there is no content in the input box, and press get to get 0.0.

In the first case, I printed Log. I (FiveActivity, onSaveInstanceState );

Next, the second case:

After we set, press get. If you do not press Back this time, press Home this time:

When we press the Home Key, our Activity is in the stop state, and our print statement appears.

At this time, we manually killed FiveActivity.

At this time, when the Activity is started, the system will find that:

The text Hello World in EditText still exists, while my Value variable changes to 0.0.

 

Summary:

1: In the first case, the Android system feels that the user is willing to act and there is no need to save it.

2: In the second case, the text in EditText exists because almost all the UIS In the Android system implement the OnSaveInstanceState method, which is the default implementation. So when we go out of Home, we will print it out. As for why our variables are not restored, it is obvious that it is because no one saves its value, which requires our programmers to manually save it.

 

When we add the following 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 way, when the process is killed, the data that comes in again will appear normally.

 

 

In addition to insufficient system resources or changes in the screen direction of the mobile phone, changing the language will cause the Activity to be recreated and then created. At this time, if we do not save the data to be saved, it will be wrong.

 

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.