You might want to read more about activity lifecycle. More you can find here (http://developer.android.com/training/basics/activity-lifecycle/index.html)
But more important is to get familiar with your activity state.
Method you are looking for is not onConfigurationChanged(Configuration newConfig)
, but onSaveInstanceState(Bundle savedInstanceState)
.
Now in your OnCreate()
method you will need to check if there is some state already saved, and then recreate that situation.
There is a very good explanation on this link: Saving Android Activity state using Save Instance State
But basically what you need to do is this:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
and then with that in mind:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Always call the superclass first
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
} else {
// Probably initialize members with default values for a new instance
}
...
}
STATE_SCORE
and STATE_LEVEL
are some public static final String
values that are used to somehow label your stuff you want to save.
For example, if you have EditText view, and you type something in, then rotate your screen, that value will be lost. But in your onSaveInstanceState()
method you shall use that Bundle
parameter and put value of your EditText view as a String.
With that saved, now you can get that value in your onCreate()
method and set the value of your EditText view to it.
Hope this helps :)