Hosting a UI Fragment

To host a UI fragment, an activity must:

  • define a spot in its layout for the fragment’s view

  • manage the lifecycle of the fragment instance

You can attach fragments to your activities in code. You determine when the fragment is added to the activity and what happens to it after that. You can remove the fragment, replace it with another, and then add the first fragment back again.

The code details will come later in the chapter. First, you are going to define MainActivity’s layout.

Defining a container view

You will be adding a UI fragment in the hosting activity’s code, but you still need to make a spot for the fragment’s view in the activity’s view hierarchy. Locate MainActivity’s layout ...

Get Android Programming: The Big Nerd Ranch Guide, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.