Chapter 7. Safe Args: Passing Information

image

Sometimes fragments need extra information to work properly.

If a fragment shows details of a contact, for example, it needs to know which contact to display. But what if this information comes from another fragment? In this chapter, you’ll build on your navigation know-how by learning how to pass data between fragments. You’ll find out how to add arguments to navigation destinations so they can receive the information they need. You’ll meet the Safe Args plug-in, and learn how to use it to write type-safe code. Finally, you’ll discover how to manipulate the back stack, and take control of back button behavior. Read on—there’s no going back…

The Secret Message app navigates between fragments

In the previous chapter, you learned how to use the Navigation component to navigate between two fragments. You used this knowledge to build the first half of the Secret Message app: an app that takes a message the user enters, and encrypts it.

The current version of the app uses a fragment named WelcomeFragment to display some introductory text. When the user clicks on its Start button, the fragment navigates to a second fragment named MessageFragment.

MessageFragment includes an edit text that lets the user enter her message. It also features a Next button, which the user clicks on to encrypt the message.

Here’s a reminder of what ...

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.