从网上看到一段代码是关于savedInstanceState保存状态的。进入程序后会执行onCreate,打出log,按home键会执行
onSaveInstanceState,打出log。
问题一:onRestoreInstanceState这个函数并没有执行,log没有打出来,这个函数在什么情况下才会执行呢问题二:按home键推出后,再返回是不会执行onCreate的,那onCreate中写了
if(null != savedInstanceState)        {            int IntTest = savedInstanceState.getInt("IntTest");            String StrTest = savedInstanceState.getString("StrTest");            Log.e(TAG, "onCreate get the savedInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);                }
是做什么用的呢?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);    }}

解决方案 »

  1.   

    onRestoreInstanceState,这个函数是在 onStart() 和 onPostCreate(Bundle).之间调用
      

  2.   

    按home键只会触发onPause(),不会触发onCreate()
      

  3.   

    那我怎么才能执行onCreate里的那段代码呢,onRestoreInstanceState又是何时调用的呢
      

  4.   

    savedInstanceState()
    onRestoreInstanceState()
    这两个方法 配合这使用, 尤其是在横竖屏切换的时候  savedInstanceState() 把信息存到Bundle 中
    然后屏幕切换完后 onRestoreInstanceState()  可以在把数据拿出来!!
      

  5.   

    onSaveInstanceState方法会在什么时候被执行,有这么几种情况:
    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方法中做数据还原
      

  6.   


    能不能举个例子让onCreate中的以下程序执行呢
    if(null != savedInstanceState)        {            int IntTest = savedInstanceState.getInt("IntTest");            String StrTest = savedInstanceState.getString("StrTest");            Log.e(TAG, "onCreate get the savedInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);                }
      

  7.   


    而且我按back键退出程序,也没有执行onRestoreInstanceState函数
      

  8.   


    这个问题,其实是一个Activity 生命周期的问题。  只要这个acitivity 进入了 onDestory() 函数(比如在这个activity中显示调用finish()函数可以达到这个效果。back 出这个应用程序也可以达到效果).那么再次进入activity 后就会调用 onCreate 函数
      

  9.   

    但是没有执行到if里面而且也没有执行到onRestoreInstanceState函数里面
      

  10.   


    这是应为 onSaveInstanceState 在activity 退出的时候没有被执行到,所以你在onCreate 里面
    if(null != savedInstanceState) 永远为null ,而没有不执行到。
      

  11.   


    This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via onCreate(Bundle) or onRestoreInstanceState(Bundle).Do not confuse this method with activity lifecycle callbacks such as onPause(), which is always called when an activity is being placed in the background or on its way to destruction, or onStop() which is called before destruction. One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact. 这是Google 对 onRestoreInstanceState的说明文档。这里对你的问题解释的很清楚
      

  12.   

    对不起,我写错了, 上面的文档是对 onSaveInstanceState 函数的说明,不是对onRestoreInstanceState的说明。根据google的文档,5楼说的调用方法是错误的。实际上onSaveInstanceState 是在activity要被GC收回前调用滴。正常的退出和 按home键,是不会调用到onSaveInstanceState 函数滴。
      

  13.   

    楼上的没试代码就说了吧
    你可以建个工程,复制下我的代码试试,按home键,是可以执行onSaveInstanceState 的,程序中有log
      

  14.   


    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)
      

  15.   

    理解这个了谢谢楼上,我想弄明白为什么,因为我知道onCreate中的if里面是进不去的,那为什么网上好多的示例程序还要那么写呢
      

  16.   


    恩恩,我的理解是这样的:
    从onSaveInstanceState()到 onRestoreInstanceState()和onCreate 中的if(null != savedInstanceState) 这套机制是android为了保证activity 没有在正常情况下finish掉(gc的回收内存等)能够有机会保存一些自己想要保存的东西。并不是所有activity一定都会触发这套机制的。onCreate 中的 if(null != savedInstanceState) 只是在触发了这套机制后会进去。没有触发(如:按back键退出程序)就不会进去。
      

  17.   

     结贴了,问题已经解决了,感谢5楼lizhengjun2010,和fishmen26说的最靠谱,研究的最认真。也谢谢其它童鞋
      

  18.   

    横竖瓶切换就是触发这套机制的好例子:onRestoreInstanceState 方法会在onStart 和 onResume之间被调用