Chapter 20. Activity fragments


This chapter covers

  • Fragment lifecycle
  • Background fragments
  • The fragment manager
  • Fragment transactions
  • The fragment back stack
  • The Android Compatibility Package

The larger screen size of tablets, which allows more content to be displayed than is possible on smaller form-factor devices such as smartphones, introduced the need for new user interface and related application components.

Recall that an Activity is an application component that is typically given a window to draw a user interface. This window typically fills the screen, which works fine on the smaller form-factor devices. Although the same approach can be used for larger-screen devices, you may have to rely on complex custom views or hierarchies, and you may have to switch across activities to maximize all the available screen real estate. The problem is compounded when you have different devices with different form factors.

Fortunately, Android 3.0 introduces a finer-grained application component called Fragment that lets you modularize the application and its user interface (into fragments). Think of fragments as “mini-activities”: reusable, independent but related mini portions of the application and screen that can be drawn independently of each other, each receiving its own events and having its own state, application lifecycle, and back stack.

20.1. Fragment lifecyle

20.2. Creating fragments and fragment layouts

20.3. Background fragments

20.4. The fragment manager

20.5. Fragment transactions

20.6. Fragment back stack

20.7. The Android Compatibility Package

20.8. Summary