Android的 - 生命周期和保存实例的状态问题 [英] Android - Lifecycle and saving an Instance State questions

查看:169
本文介绍了Android的 - 生命周期和保存实例的状态问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,在我的应用程序是创建一个新用户,与有关用户相关的细节和信息的一种形式。有有没有问题,这是用户离开活动时,没有pressing确认键只是会发生什么。

So within my application is a form for creating a new user, with relevant details and information about the user. There's no problems there, it's just what happens when the user leaves the activity without pressing the confirm button.

这就是我想要做的:

  1. 如果用户presses后退按钮,尝试所有的数据保存到数据库,并通知用户。
  2. 如果该活动被中断(即通过一个电话呼叫),保存所有的数据到临时位置,以便当活动是在再次堆栈的顶部,没有出现已经改变(但数据仍具有不尚未保存到数据库中)。
  3. 如果该活动就会被杀死更多的资源时,在后台,做同样的上述第2点(即当活动再次启动,似乎一切都没有改变)。
  4. 如果整个应用程序被再次启动(通过再次点击图标),而且无论从点2或3个以上的存储临时数据,导航到创建用户的活动,并显示数据,就好像没有任何事情改变了。

以下是我对我目前正在试图做到这一点:

  • 使用的onDestroy() isFinishing()函数找到活动时被杀害,覆盖点1以上(到然后尝试,并保存所有的数据)。
  • 保存与的onSaveInstanceState 所有数据成束(覆盖上述第2点)
  • 是否与的onSaveInstanceState 创造生存的活动包被杀害了更多的资源,所以当它再现了previous状态,可以检索(如上述第3点)?
  • 在不知道如何实现4点。
  • Use onDestroy() and isFinishing() functions to find when the activity is being killed, to cover point 1 above (to then try and save all data).
  • Save all data with onSaveInstanceState into a bundle (to cover point 2 above)
  • Does the bundle created with onSaveInstanceState survive the activity being killed for more resources, so when its recreated the previous state can be retrieved (as in point 3 above)?
  • No idea how to implement point 4.

任何帮助将是大量AP preciated。

Any help would be massively appreciated.

干杯!

推荐答案

我在很短的时间所以不能给一个完整详细的答案,但简单来说,这里是我的建议。

I'm short on time so can't give a full detailed answer, but in brief, here are my suggestions.

  • 而不是使用的onDestroy(),使用的onPause()。这是保证被调用;根据对生命周期的正式文件,只有在onPause()是保证。的onStop和的onDestroy都没有。
  • 的onSaveInstanceState和的onPause是相似的。这两个被调用时,应用程序离开前台(即进入潜在危险区域的被杀害),但与肌病不同的是临时捆绑提供。在onPause不提供任何数据存储机制,但如果你正在使用的数据库比你已经有一个。根据该文件,有没有保证,以哪一个被调用第一。
  • 在第3点),这正是OSIS是为
  • 在4点),你可以做一个临时性行在你的数据库,即保存临时数据来排1,但每当一个保存正确而成,第1行被消隐。然后在您的onCreate(),检查第1行中是否有实际的数据或为空。 (如果你的列不能为空,然后使用pre-决定,不会永远显示为正常使用的一部分占位符值。)
  • Instead of using onDestroy(), use onPause(). That's guaranteed to get called; according to the official documentation on Lifecycles, only onPause() is guaranteed. onStop and onDestroy are not.
  • onSaveInstanceState and onPause are similar. Both get called when the app leaves the foreground (i.e. it enters the potential danger zone for being killed), but the difference with oSIS is the temporary bundle it provides. onPause doesn't provide any data storage mechanism, but if you're using a database than you already have one. According to the documentation, there isn't a guaranteed order to which one gets called first.
  • Point 3) That's exactly what oSIS is meant for
  • Point 4) You could make a 'temporary row' in your database, i.e. save temporary data to row 1, but whenever a save is properly made, row 1 gets blanked. Then in your onCreate(), check whether row 1 has real data or is empty. (If your columns can't be empty, then use pre-determined placeholder values that wouldn't ever appear as part of normal use.)

这篇关于Android的 - 生命周期和保存实例的状态问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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