I'm learning basics of Android and I'm trying to save and retriever user preferences. I am using a physical device (Galaxy S4) to test the app. First I ensure the app calls the method 'onSaveInstanceState'
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt(MyPreferences.COLOR_KEY, MyPreferences.BG_COLOR);
super.onSaveInstanceState(savedInstanceState);
Log.i("i", "In onSaveInstanceState, COLOR_KEY value saved.");
}
(BG_COLOR is an int with a default value of 0).
Ones 'onSaveInstanceState' has been called, I force the app to shut down by 'CLEAR RAM' under RAM manager.
When i restart my app and onCreate is called, its parameter 'savedInstanceState' is allways null, and the Toast pop's up: "savedInstance was null".
@Override
protected void onCreate(Bundle savedInstanceState) {
if(savedInstanceState != null){
MyPreferences.BG_COLOR = (int) savedInstanceState.getInt(MyPreferences.COLOR_KEY);
Toast.makeText(MainActivity.this,"savedInstance was NOT null", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this,"savedInstance was null", Toast.LENGTH_LONG).show();
}
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
}
I wonder, have I totally misunderstood how the instance states are stored or is there some other error in my code?