Creating and using a Fragment

Android didn't always support Fragments. The early versions of Android were designed for phones, when screens had relatively small displays. It wasn't until Android started being used on tablets that there was a need to split the screen into smaller sections. Android 3.0 introduced the Fragments class and the Fragment Manager.

Along with a new class, also came the Fragment Lifecycle. The Fragment Lifecycle is similar to the Activity Lifecycle introduced in Chapter 1, Activities, as most events parallel the Activity Lifecycle.

Here's a brief overview of the main callbacks:

  • onAttach(): It's called when the Fragment is associated with an Activity.
  • onCreate(): It's called when the Fragment is first created.
  • onCreateView() ...

Get Android Application Development Cookbook - Second 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.