Chapter 6. Fragments and Navigation: Finding Your Way

image

Most apps require more than one screen.

So far, we’ve just looked at how to create single-screen apps, which is fine for simple applications. But what if you have more complex requirements? In this chapter, you’ll learn how to use fragments and the Navigation component to build multi-screen apps. You’ll learn how fragments are like subactivities with their own methods. You’ll find out how to design effective navigation graphs. Finally, you’ll meet the navigation host and navigation controller, and learn how they help you navigate from place to place.

Most apps need more than one screen

All of the apps you’ve built so far have one thing in common: they each have a single screen. Each app has a single activity with a corresponding layout, which defines the app’s appearance, and how the user can interact with it.

Most apps, however, contain more than one screen. An email app, for example, might have one screen that lets you compose an email, and another that displays a list of emails you’ve received. A calendar app might display a list of events on one screen, and the details of one specific event on another.

We’re going to show you how to build multi-screen apps by creating a Secret Message app. The app will comprise of a welcome screen, a second screen which allows the user to enter a message, and a third ...

Get Head First Android Development, 3rd 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.