在保存的onPause活动状态 [英] Saving Activity State in the onPause

查看:186
本文介绍了在保存的onPause活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我已经成功地保存和恢复使用的onSaveInstanceState可变

I have a variable that I have successfully saved and restored using onSaveInstanceState

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState); // the UI component values are saved here.
    outState.putDouble("VALUE", liter);
    Toast.makeText(this, "Activity state saved", Toast.LENGTH_LONG).show();
}

但是,这只有在activity被销毁工作。我希望通过覆盖的onPause()方法,并取回活动时不可不暂停就如何做到这一点是pciated大大AP $ P $了任何想法,以节省相同的变量

But this only works if the activity is destroyed. I want to save the same variable by overriding the onPause() method and getting back when the activity is not not paused anymore any ideas on how to do this are greatly appreciated

推荐答案

正如你已经发现,的onSaveInstanceState 只在您需要重新创建相同的所谓情况是非常有用的所谓后它已被由操作系统被破坏,通常是因为它是在后面堆太远留在记忆pressure活活动的实例。

As you have discovered, onSaveInstanceState is useful only in situations where you need to recreate the same so-called "instance" of the Activity after it has been destroyed by the OS, usually because it is too far in the back stack to stay alive under memory pressure.

保存你的数据在的onPause 确实是要走的路持久性,持续超越单一执行您的活动。为了得到这个工作,你有几种选择,包括:

Saving your data in onPause is indeed the way to go for persistence that lasts beyond a single execution of your Activity. To get this working, you have several options, including:

  • 共享preferences
  • 文件
  • 数据库
  • 内容提供商

我建议阅读本文档,以了解更多有关这些选项:

I suggest reading this documentation to learn more about each of these options:

<一个href="http://developer.android.com/guide/topics/data/data-storage.html">http://developer.android.com/guide/topics/data/data-storage.html

这篇关于在保存的onPause活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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