I've recently created my first android app. It's a really simple app that has two counters to keep scores for two teams in an IRL card game. Anyway, if the user chooses to visit other apps while having the app open like for ex. check whatsapp and have the app running in the background, it works fine, most of the time... If the user is away from the app for a longer time the app tends to reset to it's default state, meaning the score counters are both reset to 0 which is very frustrating. Is there a way to prevent this from happening? Like saving the app's state in memory until the user returns or something like that? The app counters are basic Textview objects. Thanks in advance!
我最近创建了我的第一个Android应用程序。这是一个非常简单的应用程序,有两个计数器来保持IRL纸牌游戏中两支球队的得分。无论如何,如果用户选择访问其他应用程序,同时让应用程序打开,就像前。检查whatsapp并让应用程序在后台运行,它工作正常,大部分时间......如果用户离开应用程序较长时间,应用程序往往会重置为默认状态,这意味着分数计数器都是重置为0这非常令人沮丧。有没有办法防止这种情况发生?就像在内存中保存应用程序的状态,直到用户返回或类似的东西?应用程序计数器是基本的Textview对象。提前致谢!
3 个解决方案
#1
0
You cannot stop your process from being terminated. Save your data in a database, SharedPreferences
, or some other sort of file, if you want that data to survive even after your process has been terminated.
您无法阻止您的流程被终止。如果您希望数据在您的进程终止后仍然存在,请将数据保存在数据库,SharedPreferences或其他类型的文件中。
#2
0
The state can be saved/recovered via the savedInstanceState variable.
可以通过savedInstanceState变量保存/恢复状态。
@Override public void onCreate(Bundle savedInstanceState) { }
https://developer.android.com/guide/components/activities/activity-lifecycle.html
When the user use another apps then the onPause(), onStop() event get called. After some time the OS free the memory and call onDestroy(). This is your last chance to save any state.
当用户使用其他应用程序时,将调用onPause(),onStop()事件。一段时间后,操作系统释放内存并调用onDestroy()。这是你保存任何州的最后机会。
#3
0
You need to implement onSaveInstanceState and onRestoreInstanceState
您需要实现onSaveInstanceState和onRestoreInstanceState
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("int", 1);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
int myInt = savedInstanceState.getInt("int");
}
or use library: IcePick
或使用库:IcePick
#1
0
You cannot stop your process from being terminated. Save your data in a database, SharedPreferences
, or some other sort of file, if you want that data to survive even after your process has been terminated.
您无法阻止您的流程被终止。如果您希望数据在您的进程终止后仍然存在,请将数据保存在数据库,SharedPreferences或其他类型的文件中。
#2
0
The state can be saved/recovered via the savedInstanceState variable.
可以通过savedInstanceState变量保存/恢复状态。
@Override public void onCreate(Bundle savedInstanceState) { }
https://developer.android.com/guide/components/activities/activity-lifecycle.html
When the user use another apps then the onPause(), onStop() event get called. After some time the OS free the memory and call onDestroy(). This is your last chance to save any state.
当用户使用其他应用程序时,将调用onPause(),onStop()事件。一段时间后,操作系统释放内存并调用onDestroy()。这是你保存任何州的最后机会。
#3
0
You need to implement onSaveInstanceState and onRestoreInstanceState
您需要实现onSaveInstanceState和onRestoreInstanceState
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("int", 1);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
int myInt = savedInstanceState.getInt("int");
}
or use library: IcePick
或使用库:IcePick