[英]Why onRestoreInstanceState() never gets called
I am trying to save data in my activity and than restore it.我试图在我的活动中保存数据而不是恢复它。 I save data in on
onSaveInstanceState()
and then I try to restore the data in onRestoreInstanceState()
.我将数据保存在
onSaveInstanceState()
中,然后尝试在onRestoreInstanceState()
中恢复数据。
I setup breakpoint, the method onSaveInstanceState()
get called.我设置了断点,方法
onSaveInstanceState()
被调用。 But onRestoreInstanceState()
or onCreate()
never did.但是
onRestoreInstanceState()
或onCreate()
从来没有。
Here is my steps:这是我的步骤:
Activity
.Activity
。onSaveInstanceState()
get called. onSaveInstanceState()
被调用。Activity
again.Activity
。 At this time, only onRestart()
get called.此时,只有
onRestart()
被调用。 But not onRestoreInstanceState()
or onCreate()
.但不是
onRestoreInstanceState()
或onCreate()
。
Does anyone know why?有谁知道为什么?
Well, if onRestart()
is called, the value of the instance variables would be maintained by the application stack itself and thus you do not need to restore them.好吧,如果
onRestart()
,实例变量的值将由应用程序堆栈本身维护,因此您不需要恢复它们。
onCreate()
method is only called when your Activity
's onStop()
is called and the process is killed. onCreate()
方法仅在调用Activity
的onStop()
并且进程被终止时调用。
Please refer the Activity
life cycle Android Activity Life Cycle for a clear understanding.请参考
Activity
生命周期Android Activity Life Cycle以获得清晰的理解。
You may want to check whether the onStop()
method is called and if your process is killed.您可能想检查是否调用了
onStop()
方法以及您的进程是否被终止。 I do no think that your process gets killed by the scenario which you have described.我不认为您的流程会被您所描述的场景杀死。
the onRestoreInstanceState()
method is very tricky. onRestoreInstanceState()
方法非常棘手。 I do not know when exactly it is called but I saw it was called once while changing from Potrait to Landscape.我不知道它到底是什么时候被调用的,但我看到它在从 Potrait 变为 Landscape 时被调用了一次。
Follow these steps (Using Android Studio):请按照以下步骤操作(使用 Android Studio):
Launch the app on your emulator.在您的模拟器上启动该应用程序。 You will see:
你会看见:
I/AppState﹕ onCreate I/AppState﹕onCreate
I/AppState﹕ onStart I/AppState﹕onStart
I/AppState﹕ onResume I/AppState﹕onResume
Press Ctl-F12 to rotate the emulator.按 Ctl-F12 旋转模拟器。 You will see:
你会看见:
I/StateChange﹕ onPause I/StateChange﹕onPause
I/StateChange﹕ onSaveInstanceState I/StateChange﹕onSaveInstanceState
I/StateChange﹕ onStop I/StateChange﹕onStop
I/StateChange﹕ onDestroy I/StateChange﹕onDestroy
I/StateChange﹕ onCreate I/StateChange﹕onCreate
I/StateChange﹕ onStart I/StateChange﹕onStart
I/StateChange﹕ onRestoreInstanceState I/StateChange﹕onRestoreInstanceState
I/StateChange﹕ onResume I/StateChange﹕onResume
This causes the destruction and recreation of the activity by making a configuration change to the device, such as rotating from portrait to landscape.这会通过对设备进行配置更改(例如从纵向旋转到横向)来破坏和重新创建活动。
See the link below for how to test onSaveInstanceState()
and onRestoreInstanceState()
on a real device or in the emulator.请参阅下面的链接,了解如何在真实设备或模拟器中测试
onSaveInstanceState()
和onRestoreInstanceState()
。
This method uses the AlwaysFinish setting, which is simpler and faster than killing processes.此方法使用 AlwaysFinish 设置,它比终止进程更简单、更快速。 This method also provides
Activity
-level control rather than process level control:该方法还提供了
Activity
级别的控制,而不是进程级别的控制:
http://bricolsoftconsulting.com/how-to-test-onsaveinstancestate-and-onrestoreinstancestate-on-a-real-device/ http://bricolsoftconsulting.com/how-to-test-onsaveinstancestate-and-onrestoreinstancestate-on-a-real-device/
This is my solution for real device so onRestoreInstanceState get it called.这是我针对真实设备的解决方案,因此调用了 onRestoreInstanceState。
manifest
on related activity remove this part android:configChanges="orientation"
manifest
中删除这部分android:configChanges="orientation"
manifest
on related activity remove this part android:screenOrientation="portrait"
manifest
中删除这部分android:screenOrientation="portrait"
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
then run your app, rotate your device.然后运行您的应用程序,旋转您的设备。 that's it.
而已。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.