How to do it...

To fully demonstrate working Fragments, we'll need to create two Fragments. The first Fragment will extend from ListFragment so it will not need a layout. We're going to go one step further by creating both portrait and landscape layouts for our Activity. For portrait mode, we'll swap Fragments and for landscape mode, we'll show both Fragments side by side.

When typing this code, Android Studio will offer two different library import options. Since the New Project Wizard automatically references the AppCompat library, we need to use the support library APIs instead of the framework APIs. Though very similar, the following code uses the support Fragment APIs.

Here are the steps, starting with the first Fragment:

  1. Create a new ...

Get Android 9 Development Cookbook - Third 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.