Skip to content

Instantly share code, notes, and snippets.

@noaht11
Last active August 29, 2015 14:27
Show Gist options
  • Save noaht11/d853b045feb83f25041b to your computer and use it in GitHub Desktop.
Save noaht11/d853b045feb83f25041b to your computer and use it in GitHub Desktop.
Restoring very simple state
private static final String STATE_COUNTER = "counter";
private TextView mCounterTextView;
private int mCounter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
// If we have a saved state then we can restore it now
if (savedInstanceState != null) {
mCounter = savedInstanceState.getInt(STATE_COUNTER, 0);
}
// Display the value of the counter
mCounterTextView = (TextView) findViewById(R.id.counter_text);
mCounterTextView.setText(Integer.toString(mCounter));
...
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(STATE_COUNTER, mCounter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment