简体   繁体   English

什么时候实际使用了savedInstanceState包?

[英]When is the savedInstanceState bundle actually used?

Does anyone know of an exhaustive list of when the savedInstanceState bundle will be used in an activity? 有没有人知道在活动中何时使用savedInstanceState包的详尽列表?

I know it's used when the device orientation changes. 我知道它在设备方向改变时使用。 However, it doesn't seem to be used when the user force closes the app from the Android settings, but this might be due to something in my code. 但是,当用户强制从Android设置关闭应用程序时似乎没有使用它,但这可能是由于我的代码中的某些内容。

What other cases are there? 还有哪些其他案例?

To be clear, by "used" I mean when onCreate() is called, the savedInstanceState bundle is not null and contains the data I passed into it the last time onSaveInstanceState() was called. 要清楚,通过“used”我的意思是当调用onCreate()时,savedInstanceState包不是null并且包含我上次调用onSaveInstanceState()时传入的数据。

It's used when the Activity is forcefully terminated by the OS (ex: when your Activity is in the background and another task needs resources). 当操作系统强制终止活动时使用它(例如:当您的活动在后台并且另一个任务需要资源时)。 When this happens, onSaveInstanceState(Bundle outstate) will be called and it's up to your app to add any state data you want to save in outstate . 发生这种情况时onSaveInstanceState(Bundle outstate)将被调用,它给你的应用程序添加要在保存任何状态数据outstate

When the user resumes your Activity, onCreate(Bundle savedInstanceState) gets called and savedInstanceState will be non-null if your Activity was terminated in a scenario described above. 当用户恢复您的Activity时,如果您的Activity在上述场景中被终止,则会调用onCreate(Bundle savedInstanceState)并且savedInstanceState将为非null。 Your app can then grab the data from savedInstanceState and regenerate your Activity's state to how it was when the user last saw it. 然后,您的应用程序可以从savedInstanceState获取数据, savedInstanceState Activity的状态重新生成用户上次查看时的状态。

Basically in onCreate , when savedInstanceState is null, then it means this is a 'fresh' launch of your Activity. 基本上在onCreate ,当savedInstanceState为null时,则表示这是您的Activity的“全新”启动。 And when it's non-null (if your app saved the data in onSaveInstanceState(...) , it means the Activity state needs to be recreated. 当它为非null时(如果您的应用程序将数据保存在onSaveInstanceState(...) ,则表示需要重新创建Activity状态。

onSaveInstanceState is used to store data only for application lifetime (ie temporarily) onSaveInstanceState仅用于存储应用程序生命周期的数据(即临时)

The data is held in memory only until the application is alive, in other words this data is lost when the application closes, so in your case when you force close the app onSaveInstanceState is not used. 数据仅保存在内存中,直到应用程序处于活动状态,换句话说,当应用程序关闭时,此数据会丢失,因此在您强制关闭的情况下,不会使用应用程序onSaveInstanceState

It can only be called when you do operations while your application is still alive, for eg when you change the screen orientation the activity is still intact so onSaveInstanceState is called. 它只能在应用程序处于活动状态时执行操作时调用,例如,当您更改屏幕方向时,活动仍然完好无损,因此调用onSaveInstanceState

However if you want to permanently store the data you would have to use SharedPreferences and SQLite database . 但是,如果要永久存储数据,则必须使用SharedPreferencesSQLite database

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

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