Chapter 7. Fragments and Multiplatform Support

Now that you have written some Android code you know that Activity, View, and the layout and widget subclasses of View are among the most important classes in Android. Typically, an Android user interface is built from widget views organized in layouts: a ListView in a LinearLayout, for instance. A single hierarchy of view objects gets loaded from a resource (or created by code) when an Activity is started. It is initialized and displayed on the device screen.

For small screens, this is fine: users move from screen to screen to access different parts of a program’s UI, and the Activity class (Android’s concept of a task) supports a back stack that enables quick and intuitive traversal through the strictly tree-structured interface. This changes completely, however, when the UI is spread over the surface of a larger tablet screen. Some parts of the screen remain constant over longer durations than others. Some parts of the screen determine the contents of other parts. A card-stack metaphor just doesn’t cut it.

It is entirely possible to implement UIs in which some parts of the screen change in response to activities in another part, simply by showing and hiding views. Android’s developers decided, however, that they needed more than just convention to encourage great large-screen UIs with a consistent feel and behavior. To facilitate this new kind of interaction, they introduced a new feature based on the Fragment class, as part of the ...

Get Programming Android, 2nd 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.