在activity的生命周期中,只要离开了可见阶段,或者说失去了焦点,activity就很可能被进程终止了!,被KILL掉了,,这时候,就需要有种机制,能保存当时的状态,这就是savedInstanceState的作用。
当一个Activity在PAUSE时,被kill之前,它可以调用onSaveInstanceState()来保存当前activity的状态信息(在paused状态时,要被KILLED的时候)。用来保存状态信息的Bundle会同时传给两个method,即onRestoreInstanceState() and onCreate().
示例代码如下:
- 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);
- }
- }
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xiaoxiao_job/archive/2010/08/31/5852811.aspx
My fault again.首先我承认没有试过按home键的情况,我只试过按back键的情况(确实不会执行onSaveInstanceState )所以我就根据文档和按back键的情况擅自猜测了 按home键的情况。
其实5楼说滴情况都是会调用 onSaveInstanceState 的。但是按back是不会调用的。(如果onSaveInstanceState 没有被调用,那么onRestoreInstanceState自然也不会被调用到)。
为什么back键不会触发onSaveInstanceState具体解释如下:
Android calls onSaveInstanceState() before the activity becomes vulnerable to being destroyed by the system, but does not bother calling it when the instance is actually being destroyed by a user action (such as pressing the BACK key)
横竖瓶切换就是触发这套机制的好例子:onRestoreInstanceState 方法会在onStart 和 onResume之间被调用
nSaveInstanceState方法会在什么时候被执行,有这么几种情况:
1、当用户按下HOME键时。
这是显而易见的,系统不知道你按下HOME后要运行多少其他的程序,自然也不知道activity A是否会被销毁,故系统会调用onSaveInstanceState,让用户有机会保存某些非永久性的数据。以下几种情况的分析都遵循该原则
2、长按HOME键,选择运行其他的程序时。
3、按下电源按键(关闭屏幕显示)时。
4、从activity A中启动一个新的activity时。
5、屏幕方向切换时,例如从竖屏切换到横屏时。
至于onRestoreInstanceState方法,需要注意的是,onSaveInstanceState方法和onRestoreInstanceState方法“不一定”是成对的被调用的,onRestoreInstanceState被调用的前提是,activity A“确实”被系统销毁了,而如果仅仅是停留在有这种可能性的情况下,则该方法不会被调用,例如,当正在显示activity A的时候,用户按下HOME键回到主界面,然后用户紧接着又返回到activity A,这种情况下activity A一般不会因为内存的原因被系统销毁,故activity A的onRestoreInstanceState方法不会被执行
另外,onRestoreInstanceState的bundle参数也会传递到onCreate方法中,你也可以选择在onCreate方法中做数据还原
onRestoreInstanceState,这个函数是在 onStart() 和 onPostCreate(Bundle).之间调用
http://bbs.csdn.net/topics/370064058