Android savedInstanceState的作用和用法

http://hi.baidu.com/guoxiaoming/item/d38f1303e9e1b2d21ff046ce


代码如下:

好多博客和帖子都会有下面这个例子,这个是用savedInstanceState保存的一个例子,但是仅仅这一个Activity是不是看不到效果,因为我在模拟器里面运行,按home键返回,是可以通过log看到onSaveInstanceState调用的,但是再进入应用程序是不会调用onCreate(),因为按home键失去焦点,应该是到onStop()的阶段。那么通过这个程序如何看出savedInstanceState是保存值了呢。onCreat中的if条件是进不去的。还有onRestoreInstanceState怎么才能被调用呢

package com.myandroid.test;

 

import android.app.Activity;

 

import android.os.Bundle;

 

import android.util.Log;

 

public class AndroidTest extends Activity {

 

     private static final String TAG = "MyNewLog";

 

    /** Called when the activity is first created. */

 

    @Override

 

    public void onCreate(Bundle savedInstanceState) {

 

        super.onCreate(savedInstanceState);

 

        // If an instance of this activity had previously stopped, we can

 

        // get the original text it started with.

 

        if(null != savedInstanceState)

 

        {

 

            int IntTest = savedInstanceState.getInt("IntTest");

 

            String StrTest = savedInstanceState.getString("StrTest");

 

            Log.e(TAG, "onCreate get the savedInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);        

 

        }

 

        setContentView(R.layout.main);

 

        Log.e(TAG, "onCreate");

 

    }

 

   

 

    @Override

 

    public void onSaveInstanceState(Bundle savedInstanceState) {

 

        // Save away the original text, so we still have it if the activity

 

        // needs to be killed while paused.

 

      savedInstanceState.putInt("IntTest", 0);

 

      savedInstanceState.putString("StrTest", "savedInstanceState test");

 

      super.onSaveInstanceState(savedInstanceState);

 

      Log.e(TAG, "onSaveInstanceState");

 

    }

 

   

 

    @Override

 

    public void onRestoreInstanceState(Bundle savedInstanceState) {

 

      super.onRestoreInstanceState(savedInstanceState);

 

      int IntTest = savedInstanceState.getInt("IntTest");

 

      String StrTest = savedInstanceState.getString("StrTest");

 

      Log.e(TAG, "onRestoreInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);

 

    }

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值