Why do you check for savedInstanceState == null when adding fragment?
Asked Answered
A

1

25

In the fragment doc, in one of the example, they check for savedInstanceState == null when adding a fragment:

public static class DetailsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE) {
            // If the screen is now in landscape mode, we can show the
            // dialog in-line with the list so we don't need this activity.
            finish();
            return;
        }

        if (savedInstanceState == null) {
            // During initial setup, plug in the details fragment.
            DetailsFragment details = new DetailsFragment();
            details.setArguments(getIntent().getExtras());
            getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
        }
    }
}

What is the purpose of this check? What would happen if it is not there?

Ambroseambrosi answered 11/11, 2014 at 1:6 Comment(0)
A
39

What is the purpose of this check?

To not add the fragment twice, though I prefer checking to see if the fragment is there instead of relying on that Bundle being null.

What would happen if it is not there?

Initially, nothing, as the Bundle will be null when the activity is first created.

However, then, the user rotates the device's screen from portrait to landscape. Or, the user changes languages. Or, the user puts the device into a manufacturer-supplied car dock. Or, the user does any other configuration change.

Your activity will be destroyed and recreated by default. Your fragments will also be destroyed and recreated by default (exception: those on which setRetainInstance(true) are called, which are detached from the old activity and attached to the new one).

So, the second time the activity is created -- the instance created as a result of the configuration change -- your fragment already exists, as it was either recreated or retained. You don't want a second instance of that fragment (usually), and therefore you take steps to detect that this has occurred and not run a fresh FragmentTransaction.

Ase answered 11/11, 2014 at 1:16 Comment(4)
but even though when the activity is recreated the fragment already exists, don't you still have to "re-add" it to the activity somehow? Or is that done automatically when the fragment is recreated?Ambroseambrosi
@Chin: It is done automatically when the fragment is recreated (or retained).Ase
Is savedInstanceState guaranteed to be non-null even if your app doesn't save anything to it in onSaveInstanceState()? The doc on Activity.onCreate() says: "savedInstanceState Bundle: If the activity is being re-initialized after previously being shut down then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle). Note: Otherwise it is null."Constituency
@Phil: That bit of documentation lines up with what we have seen, that the parameter passed to onCreate() on Activity will be null for first creation and non-null (though possibly empty) on re-creation after configuration change and related events. I have never seen it behave otherwise.Ase

© 2022 - 2024 — McMap. All rights reserved.