Adding Fragments to the Activity's root view

android, android-activity, android-fragments, android-view

Solution

yes it does work. The Activity root will always be a ViewGroup (otherwise how would you or the framework be able to put other views inside it when inflating the XML?)

Furthermore (to make the answer complete), it's a well known/well documented ID

getSupportFragmentManager()
    .beginTransaction()
    .replace(android.R.id.content, fragment)
    .commit();

ps.: the above code was written without looking up, so there might be some typo, somewhere.

Problem

Is it safe to assume that an `Activity`'s root view will always be a `ViewGroup`? I have an `Activity` that displays various `Fragment`s. Can I skip creating a layout XML for the `Activity` and directly add `Fragment`s to its root view?

Original source

Related problems