简体   繁体   English

如何按后退按钮保存应用程序

[英]How to save app on back button press

How can I store the state of my app when back button is pressed. 按下后退按钮时,如何存储应用程序的状态。

When back button is pressed only onPause() is called but not onSaveInstanceState where we can store our data in outState bundle. 当按下后退按钮时,仅调用onPause()但不调用onSaveInstanceState ,我们可以将数据存储在outState包中。

One answer may be sharedPreference but my problem is it will store only int and not intArray as bundles does. 一个答案可能是sharedPreference但我的问题是它只会存储int而不是intArray就像bundle一样。

Is there any way to explicitly call onSaveInstanceState ? 有没有办法显式调用onSaveInstanceState

user 600027... Even if you were able to call onSaveInstanceState the bundle would not be persisted and I do not believe it would be available after a hard kill. 用户600027 ...即使你能够调用onSaveInstanceState,捆绑也不会被持久化,我不相信它会在硬杀之后可用。 You could write a method to convert an array of ints to set of strings and then persist the data by calling putStringSet() and then write a method to covert a set of strings to an array of ints. 您可以编写一个方法将int数组转换为字符串集,然后通过调用putStringSet()来持久保存数据,然后编写一个方法将一组字符串转换为一个int数组。

JAL JAL

You can do normal saving in Activity#onDestroy() . 您可以在Activity#onDestroy()正常保存。

I think the intention behind onCreate(Bundle) , onSaveInstanceState(Bundle) and onRestoreInstanceState(Bundle) is to provide for a chance for you to save state before being killed in the background. 我认为onCreate(Bundle)onSaveInstanceState(Bundle)onRestoreInstanceState(Bundle)背后的意图是为你提供在后台被杀之前保存状态的机会。 The app should then recover and appear to the user as if nothing happened. 然后应用程序应该恢复并向用户显示,好像什么也没发生。

On a back button press the user is in some sense "finished" with the activity. 在后退按钮按下用户在某种意义上“完成”活动。 The amount of data that should be saved is different. 应保存的数据量不同。 Yes, it would be a good idea to save your state in SharedPreferences in this case. 是的,在这种情况下,将状态保存在SharedPreferences中是个好主意。

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

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