任务管理器的强制关闭删除我的应用程序的首选项,但为什么?
我正在构建我的第一个 Android 游戏,我的问题是:
当我使用手机的“后退\退出”按钮退出应用程序时,我的应用程序正在关闭,并且我需要的所有信息都很好地保存到首选项文件中:
@Override
protected void onPause()
{
super.onPause();
// My pref file
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
// Put the values to the pref file
editor.putInt("CsereHolTart", CsereHolTart);
editor.putInt("melyikAct", melyikAct);
editor.putInt("Silver", Silver);
editor.putInt("Strength", Strength);
editor.putInt("Intelligence", Intelligence);
editor.putInt("Dexterity", Dexterity);
editor.putInt("Speed", Speed);
editor.putInt("Vitality", Vitality);
editor.putInt("Morale", Morale);
editor.putInt("cHp", cHp);
editor.putInt("cMp", cMp);
editor.putInt("isGame", 1);
// Commit to storage
editor.commit();
}
看到了吗?我只是使用 onPause() 方法将我需要的所有内容实际放入首选项文件中:
public static final String PREF_FILE_NAME = "PrefFile";
当我打开应用程序时,所有保存的数据都会被很好地读取,并且我的应用程序从正确的位置继续使用我的有效值保存然后加载。
这很好,但是:
当我用手机的任务管理器关闭我的应用程序时,我手动执行“强制关闭”,似乎 onPause() 没有运行,或者是其他我不知道的东西。所以问题是当我用任务管理器关闭我的应用程序时,所有加载都失败,似乎所有保存的数据都被删除。因为每个首选项都会以这种方式加载为默认值。
谁能告诉我应该怎么做才能解决这个任务管理器的强制关闭问题? 我想在每次用户退出应用程序或打开应用程序时保存和加载。
Im building my first android game, my problem is:
When I exit from the app with my phone's "back\exit" button, my app is closing and all the information I need is nicely saved to a preference file:
@Override
protected void onPause()
{
super.onPause();
// My pref file
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
// Put the values to the pref file
editor.putInt("CsereHolTart", CsereHolTart);
editor.putInt("melyikAct", melyikAct);
editor.putInt("Silver", Silver);
editor.putInt("Strength", Strength);
editor.putInt("Intelligence", Intelligence);
editor.putInt("Dexterity", Dexterity);
editor.putInt("Speed", Speed);
editor.putInt("Vitality", Vitality);
editor.putInt("Morale", Morale);
editor.putInt("cHp", cHp);
editor.putInt("cMp", cMp);
editor.putInt("isGame", 1);
// Commit to storage
editor.commit();
}
See? I simply use the onPause() method to put all the things I need to a preference file actually to this:
public static final String PREF_FILE_NAME = "PrefFile";
When I open the app, all the saved data is well read, and my app continues from the right pont with the valid values I saved then loaded.
THIS IS GOOD, BUT:
When I close my applicaton with my phone's task manager, so i manually do a "force close", it seems onPause() is just not running, or something else I dont know. So the matter is when I close my app with task manager, all the loadings are fails, it seems all the saved data is deleted. Becuse every preference is loaded to default in that way.
Can anybody tell me what should I do to solve this task manager's force close problem?
I want to save and load everytime the user exit from the app or opens the app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您位于前台并通过某种任务管理器终止进程,
onPause() 不会被调用。仅当其他活动接管前台时才会调用
onPause()
。If you are in the foreground and kill the process via some sort of task manager,
onPause()
will not be called.onPause()
is only called when some other activity takes over the foreground.