[英]What happens to the code when someone closes or kills an app?
I have been looking into storing data for my app and trying to choose between different methods ( onSaveInstanceState , onPause/onResume ) and different methods of storing(states in onSaveInstanceState, SQLite , Prefences ). 我一直在研究为我的应用程序存储数据,并尝试在不同的方法( onSaveInstanceState , onPause / onResume )和不同的存储方法(onSaveInstanceState, SQLite , Prefences中的状态 )之间进行选择。
I am curious what happens to each of these methods of storing when the user does certain things. 我很好奇当用户做某些事情时,这些存储方法会发生什么。 In specific, I want to know what methods are called and what data is wiped when: 具体来说,我想知道在下列情况下调用哪些方法以及擦除哪些数据:
Based on my understanding, I have given my brief answer. 根据我的理解,我给出了简短的答案。 Hope this gave some insights. 希望这给了一些见解。
I'm trying to store highscores. 我正在尝试存储高分。 Currently i'm just using savedpreferences and putting 5-6 numbers in an editor, committing, and continuing. 目前我只是使用保存的偏好并在编辑器中提交5-6个数字,提交并继续。 there might be a more efficient method, which is why I elaborated on different possibilities above. 可能有一种更有效的方法,这就是我详细阐述上述不同可能性的原因。
I assume you meant SharedPreferences. 我假设你的意思是SharedPreferences。 And yes, you're doing it the right way. 是的,你正在以正确的方式做到这一点。 Your high scores will be saved between app executions. 您的高分将在应用程序执行之间保存。 And doing the same with SQLite would be way overkill. 对SQLite做同样的事情会有点矫枉过正。
The only way that data can be cleared by the user will be if he clicks on Clear the Data
or if he clicks to Uninstall
your application 用户清除数据的唯一方法是,如果他点击Clear the Data
或者他点击Uninstall
您的应用程序
However, neither uppdating the app itself, nor clicking on Clear the cache
will do anything to the SharedPreferences, the user will have to press on that button "Clear the Data" if he wants that specific data to be cleared. 但是,无论是应用程序本身,还是单击Clear the cache
都不会对SharedPreferences执行任何操作,如果用户希望清除特定数据,则必须按下“清除数据”按钮。
As to a more efficient method, it depends what you mean by that. 至于更有效的方法,它取决于你的意思。 What you're doing is what almost every developer would be doing. 你正在做的是几乎每个开发人员都会做的事情。 It's the simplest solution for the job that actually works. 对于实际工作的工作来说,这是最简单的解决方案。 For instance, if you had chosen to store that data in a bundle between activities, then all that high score data will be lost between app executions. 例如,如果您选择将这些数据存储在活动之间的捆绑中,则所有高分数据将在应用程序执行之间丢失。
That being said, your solution would obviously not be enough if you were to play the same game on multiple devices, or if you would migrate to a new device, or if you were to uninstall the game and reinstall it at a later time. 话虽这么说,如果您要在多个设备上玩相同的游戏,或者如果您要迁移到新设备,或者您要卸载游戏并在以后重新安装它,那么您的解决方案显然是不够的。 For that, you may want to consider using Google Play Services to store that data for you, both locally and on the cloud. 为此,您可能需要考虑使用Google Play服务为您(本地和云端)存储该数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.