你需要考慮你需要堅持什麼狀態。
例如;
- 哪個複選框被打勾?
- 用戶在什麼級別?
- 什麼是他們的光標
的位置作爲一個例子, 我剛完成的應用程序,其中用戶可以查看不同級別的卡片的。
我在乎;
- 他們正在尋找在哪個級別燒錄卡(也許他們在2級)
- 這卡是他們尋找的(也許他們是4卡)
這裏的碼。 HTTP:
// a variable to store the level
private final static String CARD_LEVEL_STATE = "currentCardLevel";
// a variable to store the current card
private final static String CURRENT_CARD_STATE = "currentCardNumber";
@Override
protected void onSaveInstanceState(Bundle outState) {
// save the level
outState.putInt(CARD_LEVEL_STATE, flashCardList.currentLevel());
// save the current card
outState.putInt(CURRENT_CARD_STATE, flashCardList.currentCardNumber());
// do the default stuff
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// ignore if we have no saved state
if (savedInstanceState != null) {
// record the level
if (savedInstanceState.containsKey(CARD_LEVEL_STATE)) {
int currentCardLevel = savedInstanceState.getInt(CARD_LEVEL_STATE);
flashCardList.setCardLevel(currentCardLevel);
}
// recover the current card
if (savedInstanceState.containsKey(CURRENT_CARD_STATE)) {
int currentCardNumber = savedInstanceState.getInt(CURRENT_CARD_STATE);
flashCardList.setCurrentCardNumber(currentCardNumber);
}
// refresh the view
refreshCard();
}
super.onRestoreInstanceState(savedInstanceState);
};
更多細節在這裏給出//stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state –