如何保存数据并重新打开上次使用的活动 [英] How to save data and re-open the last used Activity
问题描述
我已经完成了大部分游戏机制,现在我需要能够:
I've made the majority of my game's mechanics, I now need to be able to:
-
保存当前活动的所有数据,并在返回时检索(我想举一个
SharedPreferences
(如果我需要的话)
Save all the data of the current activity and retrieve it when coming back (I'd appreciate an example of
SharedPreferences
if that's what I need)
打开 我在同一时间从和 向左我当时是我离开的时候.
Open back the same Activity
I left from and in the same time I was when I left it.
请更清楚一点:我不想在应用程序关闭或被杀死时不从我的主要活动中重新启动.
好的,我已经使用过 Google文章为了保存我的活动并在以后重新创建.
Alright, I've used this Google article in order to save my Activity and recreate it later.
我的一项活动中的代码如下:
The code in one of my Activities is the following:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player_selection);
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
village = savedInstanceState.getString(VILLAGE);
seekBarProgress = savedInstanceState.getInt(PROGRESS);
} else {
// Probably initialize members with default values for a new instance
initializeVariables();
Bundle bundle = getIntent().getExtras();
village = bundle.getString(VILLAGE);
}
[...] // Other code skipped
}
onSaveInstanceState()
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putString(VILLAGE, village);
savedInstanceState.putInt(PROGRESS, seekBarProgress);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
因此,我创建了一个调度程序活动(现在是我的主程序),该应用程序被杀死并然后启动,知道哪个是最后一个打开的活动.但是当我尝试这样做时,它应该打开(即) PlayerSelection
活动,我得到
Thus, I've created a Dispatcher Activity (which is now my Main) that when the App is killed and then launched back, knows which is the last opened Activity to launch. But when I try to do that, and it should open (i.e.) the PlayerSelection
Activity, I get a
java.lang.NullPointerException
java.lang.NullPointerException
运行这部分代码
else {//可能使用新实例的默认值初始化成员initializeVariables();捆绑包bundle = getIntent().getExtras();村庄= bundle.getString(VILLAGE);}
else {
// Probably initialize members with default values for a new instance
initializeVariables();
Bundle bundle = getIntent().getExtras();
village = bundle.getString(VILLAGE);
}
代替之前的 if
语句,它运行 else
.
instead of the previous if
statement it runs the else
.
这是分派器活动,用于过滤应启动的活动:
This is the Dispatcher Activity which filters which Activity should be launched:
public class Dispatcher extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Class<?> activityClass;
try {
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
activityClass = Class.forName(
prefs.getString("lastActivity", MainActivity.class.getName()));
} catch(ClassNotFoundException ex) {
activityClass = MainActivity.class;
}
startActivity(new Intent(this, activityClass));
}
}
这是我发送给我的调度程序活动的信息:
This is what I send to my Dispatcher Activity:
@Override
protected void onPause() {
super.onPause();
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("lastActivity", getClass().getName());
editor.commit();
}
}
问题
为什么会发生?我该怎么做才能解决此问题?
Question
Why does it happen? What can I do to fix this?
推荐答案
为什么会发生?
在 onSaveInstanceState
中创建的 Bundle
仅保留用于短期重启,例如由于方向变化而重新创建您的活动时.如果您的活动正常停止,则此 Bundle
不会保留,因此无法传递到 onCreate
.
The Bundle
created in onSaveInstanceState
is only kept for short-term restarts, e.g. when your activity is recreated due to an orientation change. If your activity stops normally this Bundle
is not kept, and thus can't be delivered to onCreate
.
该如何解决?
将活动状态保存在 SharedPreferences
中(类似于您已经保存的 lastActivity
),而不是使用instanceState模型.您应该将状态保存在 onStop
或 onPause
中,然后分别在 onStart
或 onResume
中进行恢复.在大多数情况下,前者已足够,而后者仅产生不必要的开销.
Instead of using the instanceState model, save the activity state in SharedPreferences
(similar to the lastActivity
which you already save).
You should save your state in onStop
or onPause
and restore it in onStart
or onResume
respectively. In most cases the former is enough and the latter produces only unnecessary overhead.
@Override
protected void onStart() {
super.onStart();
String saved = sharedPreferences.getString("myKey", null);
MyPojo pojo;
if(saved == null){
pojo = defaultValue;
}else {
pojo = new Gson().fromJson(saved, MyPojo.class);
}
}
@Override
protected void onStop() {
sharedPreferences.edit().putString("myKey", new Gson().toJson(pojo)).apply();
super.onStop();
}
这篇关于如何保存数据并重新打开上次使用的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!