简体   繁体   English

为什么永远不会调用 onRestoreInstanceState()

[英]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:这是我的步骤:

  1. start my Activity .开始我的Activity
  2. press 'Home' button on the phone.按手机上的“主页”按钮。 onSaveInstanceState() get called. onSaveInstanceState()被调用。
  3. Click the icon in the launcher and launch my Activity again.单击启动器中的图标并再次启动我的Activity

At this time, only onRestart() get called.此时,只有onRestart()被调用。 But not onRestoreInstanceState() or onCreate() .但不是onRestoreInstanceState()onCreate()

Does anyone know why?有谁知道为什么?

From doc:来自文档:

The system calls onRestoreInstanceState() only if there is a saved state to restore.仅当存在要恢复的已保存状态时,系统才会调用 onRestoreInstanceState()。

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()方法仅在调用ActivityonStop()并且进程被终止时调用。

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 时被调用了一次。

I have asked similiar question earlier on here我早些时候在这里问过类似的问题

Here's some steps to test out onRestoreInstanceState() :以下是测试onRestoreInstanceState()的一些步骤:

  1. Press home screen按主屏幕
  2. Kill the app through adb通过adb杀死应用
  3. Launch your app again再次启动您的应用

Follow these steps (Using Android Studio):请按照以下步骤操作(使用 Android Studio):

  1. Create New Logcat Filter, eg AppState创建新的 Logcat 过滤器,例如 AppState
  2. 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

  3. 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。

  1. in manifest on related activity remove this part android:configChanges="orientation"在相关活动的manifest中删除这部分android:configChanges="orientation"
  2. in manifest on related activity remove this part android:screenOrientation="portrait"在相关活动的manifest中删除这部分android:screenOrientation="portrait"
  3. in your activity remove this line if it's there setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);在您的活动中删除此行,如果它在那里setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  4. on your device enable rotating from setting - display - auto rotate.在您的设备上启用旋转设置 - 显示 - 自动旋转。

then run your app, rotate your device.然后运行您的应用程序,旋转您的设备。 that's it.而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM