Android Error Activity Status

Source: Internet
Author: User

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

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.