Vlad
Vlad

Reputation: 75

What usage does savedInstanceState have in super.onCreate(savedInstanceState)?

So, I am trying to understand the inner workings of Android and I haven't found an in depth explanation of what I am looking for. For my simple program:

public class MainActivity extends AppCompatActivity {

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

What is the difference in using null instead of savedInstanceState in super.onCreate()? I have tried both and there was no difference for my program.

If there is a difference, in which ways will the usage of savedInstanceState affect my program, as opposed to using null?

Upvotes: 3

Views: 3533

Answers (1)

Ben P.
Ben P.

Reputation: 54194

The linked question/answer in @sasikumar's comment is good, and answers why you must call super.onCreate(), but it does not answer why you must pass the Bundle savedInstanceState parameter to the super call (as opposed to a new Bundle() or null).

The simple answer is that the savedInstanceState bundle contains information recorded by the Android framework that the super method will then use to restore state. For instance, any EditText with an android:id attribute will automatically save whatever the user has typed into it, and that information will be inside the savedInstanceState bundle. If you pass null, this automatic restoration is impossible.

Note that the savedInstanceState parameter will actually be null the very first time your activity is created. It will only be non-null when your activity has been destroyed and re-created (usually in response to a configuration change like the rotation of your phone, but there are many other scenarios as well).

Update

My example of automatic state restoration was incorrect. The EditText automatic restoration happens in the onRestoreInstanceState(Bundle savedInstanceState) callback, not in onCreate. Unfortunately, you can't simply pass null to that super method to see what happens (your app will crash).

However, this example will show what I meant. Assume your activity_main has some FrameLayout with an id of @+id/content, and assume you have some Fragment subclass that displays anything visible to the user. Try this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.content, new MyFragment(), "MyFragment")
                .commit();
    }
}

As is, this will display your fragment on first launch, and will continue to display the fragment after you rotate your device back and forth. This is because the FragmentManager uses the savedInstanceState bundle to save/restore fragments, and that is done in super.onCreate.

If you change the super call to super.onCreate(null), you'll still see the fragment the first time your activity starts up (since we're manually adding it at that point). But if you rotate the device, the fragment will disappear.

Upvotes: 5

Related Questions