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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.