当提交应用程序设置为共享preferences(或的onStop的onDestroy) [英] When to commit application settings to SharedPreferences (onStop or onDestroy)

查看:159
本文介绍了当提交应用程序设置为共享preferences(或的onStop的onDestroy)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,什么时候是合适的时间,以我们的应用程序设置保存到共享preferences。我们应该做的是在的onStop 的onDestroy ?我知道这两种方法各有利弊。

I was wondering, when is the suitable time to save our application settings to SharedPreferences. Should we do it during onStop, or onDestroy? I realize both methods have their pros and cons.

如果用户的意图不退出应用程序,保存应用程序设置为共享preferences只是似乎是多余的。他只是 preSS家居(适用的onStop调用) - > 长preSS家 - > <再次选择该应用程序STRONG>重新推出应用程序

If user intention is not quitting the application, save application settings to SharedPreferences just seem to be redundant. He merely press home (onStop called) -> long press home -> relaunch application by select the application again

用户可以的的通过的 preSS家居(适用的onStop调用)的应用程序 - > 长preSS家 - > < STRONG>向左滑动上的应用程序将其杀死。如果用户通过这种方式退出应用程序,我意识到的onDestroy 不会被调用虽然应用程序是的终止的。因此,应用程序设置不会被保存。

User can kill the application by press home (onStop called) -> long press home -> swipe left on the application to kill it. If user quit the application by that way, I realize onDestroy is not being called although the app is killed. Hence, application settings is not being saved.

那么,是不是更好地保存程序设置,在的onStop 的onDestroy

So, is it better to save the application settings, during onStop or onDestroy?

推荐答案

这是最好的叫提交()要么对你所做的更改后,或在的onPause()方法。这可以确保您的更改保存在pretty每很多情况下,除了崩溃您的应用程序捕获的异常。

It is best to call commit() either right after you've made the changes, or in the onPause() method. This ensures that your changes are saved in pretty much every scenario, except uncaught exceptions that crash your app.

此外,你应该注意的是,无论的onStop()的onDestroy()保证在所有被称为,尤其是在情况下,当Android是内存不足。然而,的onPause()几乎总是被命名。

Also, you should note that neither onStop() or onDestroy() are guaranteed to be called at all, particularly in situations when Android is low on memory. However, onPause() is almost always called.

这篇关于当提交应用程序设置为共享preferences(或的onStop的onDestroy)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆