简体   繁体   English

在Android中保存游戏状态的最有效方法是什么?

[英]What is the most efficient way to save game status in Android?

I have a game app that save game status in the onPause() function of the game board activity. 我有一个游戏应用程序,可将游戏状态保存在游戏板活动的onPause()函数中。

That seems very wasteful, because the user might be briefly switching to another app and return to play later. 这似乎非常浪费,因为用户可能会短暂切换到另一个应用程序并稍后再玩。 But if they never return, my app could be garbage collected and the status gets lost, so I save it just in case. 但是,如果他们再也没有回来,我的应用程序可能会被垃圾回收,并且状态会丢失,所以我保存它以防万一。

I think status gets saved many times for every time it gets read. 我认为状态每次读取都会保存很多次。 Is there a better way to lazily save the status when I know they are leaving the app? 当我知道他们离开应用程式时,有没有更好的方法来懒惰地保存状态?

Does anybody know of a guaranteed callback that I could use? 有人知道我可以使用保证的回调吗? eg if I move the save status code to onDestroy(), it might never get called. 例如,如果我将保存状态代码移到onDestroy(),则可能永远不会调用它。

Is there a way to leverage onSaveInstanceState() to help? 有没有办法利用onSaveInstanceState()提供帮助?

Thanks 谢谢

onPause() gets called when the user switches orientation and when the user navigates away. 当用户切换方向和用户离开时,会调用onPause()。 Both of these actions have the potential for data loss, so saving in onPause() is a good idea, no matter what you're making (be it a game or an app). 这两个动作都有可能导致数据丢失,因此,无论您在做什么(无论是游戏还是应用程序),保存onPause()都是一个好主意。

So no, onPause() is the safest way to ensure data persistence. 因此,不,onPause()是确保数据持久性的最安全方法。

EDIT To answer your second question, onSavedInstanceState() is not clear cut 编辑要回答您的第二个问题,onSavedInstanceState()不明确

If called, this method will occur before onStop(). 如果调用,此方法将在onStop()之前发生。 There are no guarantees about whether it will occur before or after onPause(). 无法保证它会在onPause()之前还是之后发生。 http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle ) http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle

So for a game, I would aggressively save the player's data, and would do so in onPause(). 因此,对于游戏,我会积极保存玩家的数据,并在onPause()中保存。 Everything else either has potential to fail or isn't directly related to the Activity lifecycle. 其他所有事物都有可能失败或与活动生命周期没有直接关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android最有效的保存和加载图像的方法是什么? - Android what's the most efficient way to save and load images? Android:保存 int 列表的最有效方法 - Android: Most efficient way to save a list of int 在Android中显示表格的最有效方法是什么 - What is the most efficient way to display a table in Android 使用Android相机的最有效方法是什么? - What is the most efficient way to work with the Android camera? Android-读取输入流并将其另存为文件的最有效方法是什么? - Android - Whats the most efficient way to read an inputstream and save as file? 在Android中将数据保存的最合适方法是什么 - What is the most suitable way to save that data in android 创建游戏关卡的最有效方法? (LibGDX) - Most Efficient Way to Create a Game Level? (LibGDX) 在Android用户界面中旋转和显示图像的最有效方法是什么? - What is the most efficient way to rotate and display an image in an Android UI? 在android中,比较两个文件以确定它们是否相同的最有效方法是什么? - In android, what is the most efficient way to compare two files to determine if they are identical? 什么是最有效的(电池)重复动作在android中的方法? - what is the most efficient(battery) way to repeat an action in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM