上一个问题的后续内容
在第一个屏幕活动中,我单击了一个按钮并执行以下操作
Intent intent = new Intent(MainMenu.this, NewClass.class);
intent.putExtra("value1", value1);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);值传递给新的活动,并在onCreate()-method中使用
在新的活动中,当我单击后退按钮时,我正在保存状态
@Override
protected void onSaveInstanceState(Bundle outState) {
// the UI component values are saved here.
super.onSaveInstanceState(outState);
Toast.makeText(this, "Activity state saved", Toast.LENGTH_LONG).show();
}
@Override
public void onBackPressed() {
//super.onBackPressed();
Intent intent = new Intent(RoundScoring.this, MainMenu.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Toast.makeText(this, "Back Button Pressed", Toast.LENGTH_LONG).show();
}在第一个屏幕上,我再次单击了一个不同的按钮,其中包含以下内容:
Intent intentContiune = new Intent(MainMenu.this, NewClass.class);
intentContiune.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intentContiune);Activty已加载,但始终失败,因为尚未设置该值。我编写了一个onRestorInstanceState来重新填充捆绑包中的值,但它从未触发。有人能说出我做错了什么吗?
耽误您时间,实在对不起
更新
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Log.d(DEBUG_TAG, "returnstate called");
super.onRestoreInstanceState(savedInstanceState);
value = savedInstanceState.getString("value");
}发布于 2011-08-12 19:36:32
使用应用程序
yourApplication.java
public yourApplication extends Application {
String Value;
public synchronized String getValue(){
return Value;
}
public synchronized void setValue(String Value){
this.Value = Value;
}
}AndroidManifest.xml
<application android:name="yourApplication" ...现在,您可以使用以下命令将值保存到任何位置:
保存
((yourApplication) getApplicationContext()).setValue("something");加载
((yourApplication) getApplicationContext()).getValue();发布于 2011-08-12 19:35:28
在activity重新初始化时调用onRestoreInstanceState get,而不是在activity starts.....so之后启动activity,它将不会被调用...
如果你要保存一些数据,那么你可以在这种情况下使用sharedPreference ...
发布于 2011-08-12 19:36:54
onRestoreInstanceState()是非常棘手的。我建议你使用SharedPreferences
http://developer.android.com/guide/topics/data/data-storage.html#pref
https://stackoverflow.com/questions/7039427
复制相似问题