Android的保存的onPause或游戏的onDestroy状态? [英] android save game state in onPause or onDestroy?

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

问题描述

我想实现一个游戏我正在开发一个恢复功能。它应该如下:


  • 如果用户启动游戏,但后来关闭与整理的游戏,游戏状态保存。当再次打开该应用,一个恢复选项将可用。


  • 如果该活动是唯一的暂停(例如,最小化由于电话呼叫)和用户返回时,它应该显示在正在进行的游戏。它不应该终止,并保存的状态,当然,除非操作系统决定杀活动。


我已经决定使用大部分的共享preferences,还有一个自定义的文件来保存额外的信息。我见过很多人建议保存在的onPause()方法的程序的状态,我一直在想,为什么是这种情况。

据我所知,使用的OnDestroy()会更好。 的onPause()并不意味着该活动将被杀死,所以我可以节省一定的游戏状态时浪费时间。我检查我的程序和的onDestroy()是被称为在适当的时候。我因此假设我没有泄露保持活性被破坏。

尽管这样,我找不到任何地方推荐给保存状态在的onDestroy()键,似乎每个人都使用推荐的onPause 。我缺少一条信息在这里?


解决方案

  

尽管这样,我找不到任何地方推荐给保存状态的onDestroy()和每个人似乎都用的onPause建议。我缺少一条信息在这里?


您是:)。 的onDestroy()不能保证被调用。请参见它的文档


  

注意:不要在这个方法被称为保存数据的地方算!例如,如果一个活动是在内容提供商编辑数据,这些编辑内容应在任一的onPause()或的onSaveInstanceState(包),而不是在这里提交。的这种方法通常实现为自由资源,例如那些关联的线程与活动,使破坏活动不会乱放,而其应用程序的其余仍在运行这样的事情。的在有些情况下,系统只会杀死活动的托管过程中无需调用它的这个方法(或任何其他人),因此它不应该被用来做打算事情的过程后仍然四处消失。


I am trying to implement a 'resume' feature for a game I'm developing. It should work as follows:

  • If the user starts a game and later closes the game with finishing, the game state is saved. When opening the app again, a 'resume' option will be available.

  • If the activity is only paused (e.g. minimized due to a phone call) and the user returns, it should show the game in progress. It should not terminate and save the state, unless of course, the OS decides to kill the activity.

I've decided to use SharedPreferences for the most part, as well as a custom file to save extra information. I've seen a lot of people recommend saving the state of the program in the onPause() method and I've been wondering why this is the case.

From what I can gather, using OnDestroy() would be better. onPause() does not mean the activity will be killed so I could be wasting time when saving the game state necessarily. I've checked my program and onDestroy() is being called at the appropriate times. I'm assuming therefore I have no leak keeping the activity from being destroyed.

Despite this, I cannot find anywhere recommending to save state in onDestroy() and everyone seems to recommend using onPause. Am I missing a piece of information here?

解决方案

Despite this, I cannot find anywhere recommending to save state in onDestroy() and everyone seems to recommend using onPause. Am I missing a piece of information here?

You are :). onDestroy() is not guaranteed to be called. See the documentation for it:

Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

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

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