我正在使用android中的选项卡式应用程序。在第一个标签中我有一个带有textviews,spinners的搜索引擎..如果我填写了这些参数并切换到其他标签或搜索结果活动,我返回到此标签(搜索引擎标签)我发现所有这些字段已重新初始化。但我想保留它们的价值,以便记住我搜索的内容.. 当我在活动之间切换时,有没有办法保存我的数据?
感谢您的帮助。
答案 0 :(得分:1)
您可以覆盖onSaveInstanceState(将在转换到新活动之前调用)和onRestoreInstanceState(将在您的活动恢复之前调用),例如:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putBoolean("MyBoolean", true);
savedInstanceState.putDouble("myDouble", 1.9);
savedInstanceState.putInt("MyInt", 1);
savedInstanceState.putString("MyString", "Welcome back to Android");
// etc.
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
double myDouble = savedInstanceState.getDouble("myDouble");
int myInt = savedInstanceState.getInt("MyInt");
String myString = savedInstanceState.getString("MyString");
}
答案 1 :(得分:0)
我猜您正在创建意图对象,以addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
启动标签的活动。如果您已经完成了这样的操作,请删除此功能。例如,创建如下所示的intent对象,
Intent intent = new Intent(this, tabActivity.class);
不喜欢这个,
Intent intent = new Intent(this, tabActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
如果您这样做,在切换到另一个标签之前,您将获得标签的确切状态。