简体   繁体   English

当某人关闭或杀死某个应用时代码会发生什么变化?

[英]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 ). 我一直在研究为我的应用程序存储数据,并尝试在不同的方法( onSaveInstanceStateonPause / onResume )和不同的存储方法(onSaveInstanceState, SQLitePrefences中的状态 )之间进行选择。

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: 具体来说,我想知道在下列情况下调用哪些方法以及擦除哪些数据:

  • User clicks the task switcher button(bottom right) and then closes the overlay within seconds, all from within the app 用户单击任务切换器按钮(右下角),然后在几秒钟内关闭覆盖,全部来自应用程序内部
  • User clicks home without swiping out app from task switcher, then reopens app 用户点击主页而无需从任务切换器中清除应用程序,然后重新打开应用程序
  • User clicks home, swipes out the app from task switcher, then reopens app 用户点击主页,从任务切换器中清除应用程序,然后重新打开应用程序
  • User exits app, restarts phone, then opens app 用户退出应用程序,重新启动手机,然后打开应用程序
  • User exits app, and uses Clean Master (or any other storage manager) to clear the cache of all apps or kill all background tasks, then reopens app. 用户退出应用程序,并使用Clean Master (或任何其他存储管理器)清除所有应用程序的缓存或终止所有后台任务,然后重新打开应用程序。
  • User updates app 用户更新应用
  1. When the user goes to task switcher and closes the app, then the app process will get killed and any services running in the background will also be stopped 当用户转到任务切换器并关闭应用程序时,应用程序进程将被终止,并且后台运行的任何服务也将被停止
  2. When the user clicks home, without swiping out from the task switcher and reopens the app, then the app is getting resumed. 当用户点击主页时,无需从任务切换器中刷出并重新打开应用程序,然后应用程序将恢复运行。 You can get further details if you search for activity lifecycle 如果搜索活动生命周期,您可以获得更多详细信息
  3. When the user clicks home and swipes out the app from task switcher, then it is equivalent to killing the app and the process will get killed and any services running in the background will also be stopped 当用户点击主页并从任务切换器中清除应用程序时,它相当于杀死应用程序,并且该进程将被终止,并且后台运行的任何服务也将被停止
  4. When the user exits the app and restarts.reboots the phone, then the process is killed and services also will get killed. 当用户退出应用程序并重新启动时,请重新启动手机,然后该进程将被终止并且服务也将被终止。 But after restarting/rebooting the device, user would have written logic to restart it once the device is restarted or app is killed 但是在重新启动/重新启动设备后,一旦设备重新启动或应用程序被杀,用户就会编写逻辑来重新启动它
  5. When the user exits the app and clears cache, then I would assume that the data is cleared. 当用户退出应用程序并清除缓存时,我会假设数据已清除。 So this is like a fresh app. 所以这就像一个新鲜的应用程序。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM