onCreate中的savedInstanceState有何具体作用

   在activity的生命周期中,只要离开了可见阶段,或者说失去了焦点,activity就很可能被进程终止了!,被KILL掉了,,这时候,就需要有种机制,能保存当时的状态,这就是savedInstanceState的作用。

      当一个Activity在PAUSE时,被kill之前,它可以调用onSaveInstanceState()来保存当前activity的状态信息(在paused状态时,要被KILLED的时候)。用来保存状态信息的Bundle会同时传给两个method,即onRestoreInstanceState() and onCreate().

示例代码如下:

[java]  view plain copy
  1. package com.myandroid.test;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6.   
  7. public class AndroidTest extends Activity {  
  8.     private static final String TAG = "MyNewLog";  
  9.     /** Called when the activity is first created. */  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         // If an instance of this activity had previously stopped, we can  
  14.         // get the original text it started with.  
  15.         if(null != savedInstanceState)  
  16.         {  
  17.             int IntTest = savedInstanceState.getInt("IntTest");   
  18.             String StrTest = savedInstanceState.getString("StrTest");   
  19.             Log.e(TAG, "onCreate get the savedInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);    
  20.         }  
  21.   
  22.         setContentView(R.layout.main);  
  23.         Log.e(TAG, "onCreate");  
  24.     }  
  25.   
  26.     @Override   
  27.     public void onSaveInstanceState(Bundle savedInstanceState) {   
  28.       // Save away the original text, so we still have it if the activity  
  29.       // needs to be killed while paused.  
  30.       savedInstanceState.putInt("IntTest"0);   
  31.       savedInstanceState.putString("StrTest""savedInstanceState test");   
  32.       super.onSaveInstanceState(savedInstanceState);   
  33.       Log.e(TAG, "onSaveInstanceState");  
  34.     }   
  35.   
  36.     @Override   
  37.     public void onRestoreInstanceState(Bundle savedInstanceState) {   
  38.       super.onRestoreInstanceState(savedInstanceState);   
  39.   
  40.       int IntTest = savedInstanceState.getInt("IntTest");   
  41.   
  42.       String StrTest = savedInstanceState.getString("StrTest");   
  43.   
  44.       Log.e(TAG, "onRestoreInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);  
  45.     }   
  46. }  

 

本文来自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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值