You need to consider what state you need to persist.
For example;
- which checkboxes have been ticked?
- what level is the user on?
- what is the position of their cursor
As an example,
I just finished an application where a user can view different levels of flashcards.
I care about;
- which level of flashcard they are looking at (maybe they're at level 2)
- which card are they looking at (maybe they're on the 4th card)
Here's the code.
// 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);
};