当该savedInstanceState束实际使用?实际、savedInstanceState

2023-09-12 22:18:06 作者:半宛清愁

有谁知道,当savedInstanceState束在活动中会使用一个详尽的列表?

Does anyone know of an exhaustive list of when the savedInstanceState bundle will be used in an activity?

我知道它使用的设备的方向改变时。但是,它似乎并不当用户关闭力从Android设置应用程序中使用,但是这可能是由于东西在我的code。

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.

还有什么其他情况有哪些?

What other cases are there?

需要明确的是,通过拿来主义我的意思是,当的onCreate()被调用时,savedInstanceState包不为空,并且包含我传递给它的最后一次数据的onSaveInstanceState()被调用。

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.

推荐答案

它使用时的活动被强行由操作系统(例如:当你的活动是在后台进行,另一项任务需要的资源)终止。发生这种情况时,的onSaveInstanceState(捆绑outstate)将被调用,这完全取决于你的应用程序添加要保存 outstate任何状态数据

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.

当用户恢复您的活动,的onCreate(包savedInstanceState)被调用和 savedInstanceState 将非空如果你的活动被终止在上述的场景。然后,您的应用程序可以抓住从 savedInstanceState 的数据,并重新生成活动的状态究竟是怎么回事,当用户最后一次看到它。

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

基本上在的onCreate ,当 savedInstanceState 为空,那么就意味着这是一个新鲜推出的你活动。当它的非空(如果您的应用程序保存在的onSaveInstanceState(...),这意味着活动状态,需要重新创建。

Basically in onCreate, when savedInstanceState is null, then it means this is a 'fresh' launch of your Activity. And when it's non-null (if your app saved the data in onSaveInstanceState(...), it means the Activity state needs to be recreated.