You've to implement the callback onSaveInstanceState
something like this -
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("param1", yourGlobalIntVariableName);
outState.putString("param2", yourGlobalStringVariableName);
}
And in your onCreate
you should see if your savedInstanceState
variable is not null and in case it is not null, you've to take the saveInstanceState
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null) {
yourGlobalIntVariableName = savedInstanceState.getInt("param1");
yourGlobalStringVariableName = savedInstanceState.getString("param2"));
}
}
That should fix the problem.
If you have a Android 4.0 test device, go to Settings -> Developer Options -> "Do not keep activities"
Check it. And then run your app. See if it works fine. Ideally it should. OS can kill some of those activities in your back stack and your app should be able to handle it. This developer option will give you a way to test such cases. It removes the previous activity out of memory as soon as you go to the next activity.