Monstieur
Monstieur

Reputation: 8112

Adding Fragments to the Activity's root view

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

Upvotes: 2

Views: 2798

Answers (1)

Budius
Budius

Reputation: 39836

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.

Upvotes: 7

Related Questions