Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Modern Android 13 Development Cookbook

You're reading from  Modern Android 13 Development Cookbook

Product type Book
Published in Jul 2023
Publisher Packt
ISBN-13 9781803235578
Pages 322 pages
Edition 1st Edition
Languages
Author (1):
Madona S. Wambua Madona S. Wambua
Profile icon Madona S. Wambua

Table of Contents (15) Chapters

Preface 1. Chapter 1: Getting Started with Modern Android Development Skills 2. Chapter 2: Creating Screens Using a Declarative UI and Exploring Compose Principles 3. Chapter 3: Handling the UI State in Jetpack Compose and Using Hilt 4. Chapter 4: Navigation in Modern Android Development 5. Chapter 5: Using DataStore to Store Data and Testing 6. Chapter 6: Using the Room Database and Testing 7. Chapter 7: Getting Started with WorkManager 8. Chapter 8: Getting Started with Paging 9. Chapter 9: Building for Large Screens 10. Chapter 10: Implementing Your First Wear OS Using Jetpack Compose 11. Chapter 11: GUI Alerts – What’s New in Menus, Dialog, Toast, Snackbars, and More in Modern Android Development 12. Chapter 12: Android Studio Tips and Tricks to Help You during Development 13. Index 14. Other Books You May Enjoy

Navigation in Modern Android Development

In Android development, navigation is the interaction that allows your Android application users to navigate to, from, and back out from the different screens within your app, an action that is very vital in the mobile ecosystem.

Jetpack navigation has simplified navigation between screens, and in this chapter, we will learn how to implement navigation with a simple view click, from the bottom navigation bar, which is most commonly used, by navigating with arguments, and more.

In this chapter, we’ll cover the following recipes:

  • Implementing a bottom navigation bar using navigation destinations
  • Navigating to a new screen in Compose
  • Navigating with arguments
  • Creating deep links for destinations
  • Writing tests for navigation

Technical requirements

Implementing a bottom navigation bar using navigation destinations

In Android development, having a bottom navigation bar is very common; it helps inform your users that there are different sections in your application. In addition, other apps opt to include a navigation drawer activity, which holds a profile and additional information about the application.

An excellent example of an app that utilizes both – a navigation drawer and bottom navigation – is Twitter. It is also important to mention that some companies prefer to have a top navigation bar as a preference. In addition, others such as Google Play Store have both bottom and drawer navigation.

Getting ready

Create a new Android project with your preferred editor or Android Studio, or you can use any project from previous recipes.

How to do it…

In this recipe, we are going to create a new project and call it BottomNavigationBarSample:

  1. After creating our new empty Activity BottomNavigationBarSample...

Navigating to a new screen in Compose

We will build a register screen prompt on our login page for registering first-time users of our application. This is a standard pattern because we need to save the user’s credentials so that the next time they log in to our application, we just log them in without registering again.

Getting ready

You should have completed the previous recipe, Implementing a bottom navigation bar using navigation destinations, before getting started with this one.

How to do it…

In this recipe, we will need to use our SampleLogin project and add a new screen that users can navigate to if it is their first time using the application. This is a typical use case in many applications:

  1. Open your SampleLogin project, create a new sealed class, and call it Destination. To also ensure we maintain great packaging, add this class to util. Also, just like the bottom bar, we will have a route, but this time, we do not need any icons or titles...

Navigating with arguments

Passing data between destinations is very vital in Android development. The new Jetpack navigation allows developers to attach data to a navigation operation by defining an argument for a destination. Readers will learn how to pass data between destinations using arguments.

A good use case is, say, you load an API with data and want to show more description on the data you just displayed; you can navigate with unique arguments to the next screen.

Getting ready

We will explore the most common interview project requirement, which is to fetch data from an API and display one screen and add an additional screen for extra points.

Let’s assume the API is the GitHub API, and you want to display all organizations. Then, you want to navigate to another screen and see the number of repositories each company has.

How to do it…

For this recipe, we will look at an example of navigating with arguments as a concept since there is little more...

Creating deep links for destinations

In Modern Android Development, deep links are very vital. A link that helps you navigate directly to a specific destination within an app is called a deep link. The Navigation component lets you create two types of deep links: explicit and implicit.

Compose navigation supports implicit deep links, which can be part of your Composable functions. It is also fair to mention there is no huge difference between how you would handle these using XML layouts.

Getting ready

Since we don’t have a deep link use case in our application, in this recipe, we will look into how we can utilize the knowledge by learning how to implement implicit deep links.

How to do it…

You can match deep links using a Uniform Resource Locator (URI), intent actions, or Multipurpose Internet Mail Extensions (MIME) types. Furthermore, you can easily specify multiple types that match for a deep link single but remember that the URI argument comparison is...

Writing tests for navigation

Now that we have created a new screen for our SampleLogin project, we need to fix the broken test and add new tests for the UI package. If you can recall, in Chapter 3, Handling the UI State in Jetpack Compose and Using Hilt, we did unit tests and not UI tests. This means after adding all the ViewModel instances, our UI tests are now broken. In this recipe, we will fix the failing tests and add a navigation test.

Getting ready

In this recipe, you do not need to create any new project; use the already-created project, SampleLogin.

How to do it…

You can apply these concepts to test the bottom navigation bar we created. Hence, we will not be writing tests for the BottomNavigationBarSample project. Open SampleLogin and navigate to the androidTest package. We will add tests here for the new RegisterScreen() a Composable function, and also fix the broken tests:

  1. Let’s open the LoginContentTest class. Now, let’s move the...
lock icon The rest of the chapter is locked
You have been reading a chapter from
Modern Android 13 Development Cookbook
Published in: Jul 2023 Publisher: Packt ISBN-13: 9781803235578
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}