what does super.onSaveInstanceState() do inside an overridden onSaveInstanceState()

android, android-activity, java

Solution

When you call `super.onSaveInstanceState()` the state of all your `View`s is saved.

If you don't call the `super` method your code will still work...until...

...until your application is put in the background because the user presses HOME or runs another application (via notification or whatever) and then Android kills your application's process because it has been in the background for awhile (or it needs the resources, or it just wants to make your life as a developer difficult).

Then, when the user returns to your application (by launching it again, or selecting it from the list of recent tasks), Android will happily create a new process for your application and launch your Activity again and pass it the saved instance bundle (which now doesn't have the saved state of all your `View`s because you forgot to call `super`) and your Activity won't be shown to the user in the same state as he last left it. It will be shown to the user with the initialized (default, empty, starting) state.

This is why you always need to call `super.onSaveInstanceState()` and `super.onRestoreInstanceState()` when you override these methods.

Problem

I am new to Android development. I was using overriden version of `onSaveInstanceState()` to save my own app data. I noticed that I did not call `super.onSaveInstanceState(savedInstanceState)` inside my function and the code worked fine. ``` @Override public void onSaveInstanceState(Bundle savedInstanceState) { // TODO: // Save state information with a collection of key-value pairs // 4 lines of code, one for every count variable savedInstanceState.putInt(CREATE_KEY, mCreate); savedInstanceState.putInt(RESUME_KEY, mResume); savedInstanceState.putInt(RESTART_KEY, mRestart); savedInstanceState.putInt(START_KEY, mStart); } ``` I was wondering if `super.onSaveInstanceState(savedInstanceState)` is called implicitly ? Also, what is the purpose of calling `super.onSaveInstanceState(savedInstanceState)` inside the overridden function.

Original source