I recently learned about the Android SDK, not very clear about some of the principles, how to save the state of the application, look at the following Hello small example:
| 12345678910111213141516171819202122232425 |
package com.android.hello;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class HelloAndroid extendsActivity {/** Called when the activity is first created. */@Overridepublicvoid onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mTextView =new TextView(this);if(savedInstanceState == null) {mTextView.setText("Welcome to HelloAndroid!");}else {mTextView.setText("Welcome back.");}setContentView(mTextView);}privateTextView mTextView = null;} |
This Android small example should have been developed by everyone, every time I close the program and then open is to show me the first Welcome to Helloandroid, I would like to be able to display the second visit Welcome back, I how to deal with?
Processing methods
You need to rewrite Onsaveinstancestate (bundle savedinstancestate) and write the state you want to save into the bundle, as follows
| 123456789101112 |
@Overridepublic void onSaveInstanceState(Bundle savedInstanceState) {super.onSaveInstanceState(savedInstanceState);// Save UI state changes to the savedInstanceState.// This bundle will be passed to onCreate if the process is// killed and restarted.savedInstanceState.putBoolean("MyBoolean",true);savedInstanceState.putDouble("myDouble",1.9);savedInstanceState.putInt("MyInt",1);savedInstanceState.putString("MyString","Welcome back to Android");// etc.} |
The essence of bundles is the way that key-value pairs are stored, and you can pass OnCreate () and onrestoreinstancestate () or the corresponding values
| 12345678910 |
@Overridepublic void onRestoreInstanceState(Bundle savedInstanceState) {super.onRestoreInstanceState(savedInstanceState);// Restore UI state from the savedInstanceState.// This bundle has also been passed to onCreate.booleanmyBoolean = savedInstanceState.getBoolean("MyBoolean");doublemyDouble = savedInstanceState.getDouble("myDouble");intmyInt = savedInstanceState.getInt("MyInt");String myString = savedInstanceState.getString("MyString");} |
You will typically use this technique to store the value of an instance of an application (selection, unsaved text, and so on).
Original address: http://www.itmmd.com/201410/38.html
This article by Meng Meng's IT person to organize the release, reprint must indicate the source.
Android Error Activity Status