从网上看到一段代码是关于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); }}
解决方案 »
- 关于云储存或者百度云的基础问题, 用java/android 实现上传文件到云储存(比如百度云)
- Android如何实现进度条-进度圆角
- 我想限制返回键返回某个界面,请问这个可以时间吗
- android自带浏览器,播放网页视频MV时,按home键退出到桌面,再进去浏览器后,黑屏
- android 安装问题
- ExpandableListView中如何删除指定位置的child元素?
- JNI System.loadLibrary后调用函数出错.
- android线程问题2(上个帖子描述不清,现在详细描述)
- 自定义ScrollLayout继承ViewGroup动态添加gridView,长按冲突
- android 模拟登录教务系统 获取课程表 出错
- 有谁知道 android 4.* vpn的!!!, 调用了 IConnectivityManager.aidl 时,无法import几个隐藏类,报错
- 有关Adapter的解释
onRestoreInstanceState()
这两个方法 配合这使用, 尤其是在横竖屏切换的时候 savedInstanceState() 把信息存到Bundle 中
然后屏幕切换完后 onRestoreInstanceState() 可以在把数据拿出来!!
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方法中做数据还原
能不能举个例子让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); }
而且我按back键退出程序,也没有执行onRestoreInstanceState函数
这个问题,其实是一个Activity 生命周期的问题。 只要这个acitivity 进入了 onDestory() 函数(比如在这个activity中显示调用finish()函数可以达到这个效果。back 出这个应用程序也可以达到效果).那么再次进入activity 后就会调用 onCreate 函数
这是应为 onSaveInstanceState 在activity 退出的时候没有被执行到,所以你在onCreate 里面
if(null != savedInstanceState) 永远为null ,而没有不执行到。
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的说明文档。这里对你的问题解释的很清楚
你可以建个工程,复制下我的代码试试,按home键,是可以执行onSaveInstanceState 的,程序中有log
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)
恩恩,我的理解是这样的:
从onSaveInstanceState()到 onRestoreInstanceState()和onCreate 中的if(null != savedInstanceState) 这套机制是android为了保证activity 没有在正常情况下finish掉(gc的回收内存等)能够有机会保存一些自己想要保存的东西。并不是所有activity一定都会触发这套机制的。onCreate 中的 if(null != savedInstanceState) 只是在触发了这套机制后会进去。没有触发(如:按back键退出程序)就不会进去。