你需要重寫的onSaveInstanceState(捆綁savedInstanceState)和寫要更改的捆綁參數這樣的應用程序的狀態值:
@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);
}
的包基本上是一種存儲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");
}
您通常會使用這種技術來存儲應用程序的實例值(選擇,未保存的文本等)。
取一些變量並存儲它的值。 –
我在我的答案編輯示例鏈接.. – user370305
看看我編輯的答案,我給一段代碼.. – user370305