Fragments
In this section, we are going to review briefly the key concepts of fragments to explain advanced features and components, such as Fragment Manager and the fragments back stack.
In our example, we will create an activity called MainActivity
and four fragments: ListFragment
, ContactFragment
, SettingsFragment
, and DetailsFragment
. For this, you can create a fragments
package and double-click on the package to go to New | Fragment | Blank Fragment. Take a look at the following dialog box:
For now, you can create them without the fragment factory methods and the interface callbacks. We will cover these later in the chapter.
Our project so far should look like this in the Project view:
Understanding the importance of fragments
A fragment represents a behavior or a portion of the user interface in an activity. You can combine multiple fragments in a single activity to build a multipane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an...