你需要重寫onSaveInstanceState(Bundle savedInstanceState)
,寫你想改變的參數捆綁像這樣的應用程序的狀態值:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(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.
}
的包基本上存儲的方式NVP(「名稱 - 值對」)的地圖,並將在獲得通過到的onCreate(),也onRestoreInstanceState(),您會提取這樣的價值觀:
@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");
}
你通常會使用這種技術來存儲您的應用的實例值(選擇,未保存的文本等)。
小心:官方文檔指出,您應該將重要信息保存在onPause-Method中,因爲onsaveinstance-method不是android生命週期的一部分。 developer.android.com/reference/android/app/Activity.html