如何使用的onPause与Android?如何使用、onPause、Android

2023-09-05 00:28:10 作者:又污又可爱

使用的onSaveInstanceState(包..)和onRestoreInstanceState(包..) 是非常好的工作,但在工作时点击关闭按钮模拟器。 现在,我想保存状态,并恢复保存的数据时,下面的按钮用于:

using onSaveInstanceState(Bundle ..) and onRestoreInstanceState(Bundle ..) was really good and work, but it is working when click Turn off button on Emulator. Now, i want to save state and restore the saved data when below button used:

我认为这是可能的使用将OnPause()或oOnStop(),如果我是正确的,如何使用它, 这将是足以让我保存一个布尔变量的Java源代码,并恢复它, 谢谢你。

I think it is possible to use OnPause() OR oOnStop(), if i am right, How to use it, it will be enough to show me Java source of saving one boolean variable, and restore it, Thanks.

推荐答案

我会使用的onPause(),因为的onStop()不能保证被调用。请参阅应用程序基础有关生命周期的详细信息。

I would use onPause(), as onStop() is not guaranteed to be called. See the application fundamentals for details on the lifecycle.

要保存和恢复一个布尔值,我会用共享preferences 。有在数据存储页面一个code为例它展示了如何保存和恢复一个布尔值。他们使用的onCreate()的onStop(),但我会用 onResume()的onPause(),因为我已经提到的原因。

To save and restore a boolean, I would use SharedPreferences. There is a code example on the data storage page that shows how to save and restore a boolean. They use onCreate() and onStop(), but I would use onResume() and onPause(), for the reasons I have already mentioned.