Could the savedInstanceState
parameter passed to Activity.onCreate
have been saved by a different version of the app? I can't find any reference to this in the documentation.
This could be theoretically possible if the activity was backgrounded, then killed due to low memory, then a new version of the app was installed, and then the user navigated back to the activity using the recents list.
In the example code below, could onCreate
throw an ArrayIndexOutOfBoundsException
or set a surprising state if the enum has been changed between versions, or is the code safe?
public class ExampleActivity extends Activity {
private enum State {
SOMETHING,
SOMETHING_ELSE,
ANOTHER_THING,
ET_CETERA,
}
private State state;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
state = State.values()[savedInstanceState.getInt("state")];
} else {
state = State.ANOTHER_THING;
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("state", state.ordinal());
}
// ...
}