Android 上在整个应用程序会话的活动死亡/重新启动之间保留数据的最佳做法是什么? [英] What is the best practices on Android to keep data between activities deathes/restarts for the whole application session?

查看:19
本文介绍了Android 上在整个应用程序会话的活动死亡/重新启动之间保留数据的最佳做法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在设计一个 Android 应用,其中包含多个以类似向导的方式工作的 Activity - 用户应该从 Activity #1 传递到 Activity #5 以到达最终的 Activity (#6).

We're designing an Android app that has several activities which are working in a wizard like way - user should pass from the activity #1 to activity #5 to get to the final activity (#6).

由于我们知道 Activity 可能会在内存不足的情况下被操作系统突然终止,因此我们使用 Application 类作为静态存储来保存用户在向导"活动中输入的数据以及我们的应用程序在整个会话中需要的其他数据.

Since we know an activity can be suddenly terminated by OS on low memory we used Application class as a static storage for keeping the data the user inputs on "wizard" activities and other data our app needs for the whole session.

不幸的是,我们发现这种方法失败了 - 看起来 Application 类实例也可以被操作系统杀死(这是在 Android 1.6 和 1.5 上特别发现的).我们对这种方法的期望是否错误(我们认为 Application 类实例始终存在于整个应用会话中)?

Unfortunately we've discovered this approach fails - looks like the Application class instance is also can be killed by OS (this was specifically discovered on Android 1.6 versus 1.5). Are our expectations wrong on this approach (we think Application class instance always lives for the whole app session)?

所以问题是 - Android 上在整个应用程序会话的活动死亡/重新启动之间保留数据的最佳做法是什么?

So the question is - what is the best practices on Android to keep data between activities deathes/restarts for the whole application session?

推荐答案

我们正在设计一款 Android 应用有几个活动是以类似向导的方式工作 - 用户应该从活动 #1 传递到活动 #5 进入决赛活动(#6).

We're designing an Android app that has several activities which are working in a wizard like way - user should pass from the activity #1 to activity #5 to get to the final activity (#6).

您确定应该将它们作为单独的活动来实施吗?为什么不是单个活动,使用 ViewFlipper 或其他东西在向导状态之间移动?

Are you sure you should be implementing them as separate activities? Why not a single activity, using ViewFlipper or something to move between the wizard states?

我们对此的期望是错误的吗方法(我们认为应用程序类实例总是为整体而存在应用会话)?

Are our expectations wrong on this approach (we think Application class instance always lives for the whole app session)?

应用程序类实例在整个应用程序会话中始终存在,AFAIK.但是,一旦您的所有活动都被销毁,应用程序也将被销毁.

The Application class instance does always live for the whole app session, AFAIK. Once all your activities are destroyed, though, the Application will be destroyed as well.

所以问题是 - 什么是最好的Android 上的数据保存实践活动死亡/重启之间整个应用程序会话?

So the question is - what is the best practices on Android to keep data between activities deathes/restarts for the whole application session?

  1. 使用 Service -- 如果这被踢出 RAM,只需从头开始
  2. 使用数据库
  3. 使用文件
  1. Use a Service -- if this gets kicked out of RAM, just start over from scratch
  2. Use a database
  3. Use a file

或者,最好:在这种情况下不要使用多个活动.向导是一个单一的逻辑事物;如此对待它并使其成为一个单一的活动.通过 onSaveInstanceState() 保持你的状态,你就设置好了.

Or, best: don't use multiple activities in this case. A wizard is a single logical thing; treat it as such and make it be a single activity. Persist your state via onSaveInstanceState(), and you're set.

这篇关于Android 上在整个应用程序会话的活动死亡/重新启动之间保留数据的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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