Towards eternal beauty the natural way

Women world over are incredible multi-taskers! Juggling nimbly the 9 to 5 work schedules, kids, kitchen, traffic, gym, you can’t name enough in fact! Spiralling down the 20s onto the 30s and our skin…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Jetpack Compose Navigation Architecture

Lets go through Navigation Component in Compose with an example code and write UI Tests for Navigation Component.

Navigation in Compose has the same key parts as traditional View base Navigation, lets see basic description and will go in details further down

you do not need to use requireActivity to get NavController, you can create it via Api rememberNavController() in composable.

route is a key used to defined each composable destination where you can navigate to using navController, you can think of it as a deep link to the composable. It should uniquely identify each composable destination

lets get to code example.

As an example we will use a sample project of a music app which has three screens, Songs Screen, Favourites Screen and Search Screen.

To show usage of navigation we will keep it simple and will create a Home screen which will have three buttons on it to navigate to each of the above mentioned screens above.

HomeScreen is created to show navigation, In real projects you would use bottom navigation or drawer navigation based on your need

In our example, we will create a NavHost with four compose destinations using keyword composable, specifying each destination route and composable to which it will be navigated. We will pass startDestination in NavHost as “home” route, HomeScreen composable will show up as first screen

Lets setContent in our main activity as MainNavigation compsable

Launch the App and you will see following screen

NavHost acts as container which currently is showing HomeScreen as “home” is set as Start destination for NavHost

In order to navigate to a composable destination in Navigation Grpah, you will use navigate method on navController and passing in route of the destination where you want to navigate

The above example is navigating by passing route as “songs” so it will navigate to the SongsScreen composable, check above how our navigation graph is created.

In our example HomeScreen composable will navigate to SongsScreen, FavouritesScreen and SearchScreen composables.

How to achieve this? You can achieve this by exposing Lambda functions rather than passing in navController as parameter down in hierarchy. see below…

In our example we want to navigation from HomeScreen composable to three other composables, so we will expose three lambda methods from HomeScreen composable onNavigateToSongs, onNavigateToFavourites, onNavigateToSearch and on each button press we will call these corresponding lambdas which will eventually navigate to that particular composable destination, See the code below.

see changes in HomeScreen composable as explained above

Corresponding changes in MainNavigation/NavHost,

we are managing all navigations on higher level in MainNavigation composable and its easy to read and scale

Run the app you will see navigations working and device back button will also work automatically. Pressing device back button will pop back top composable on back stack which is being managed internally by NavController.

Lets say we want to navigate from SearchScreen composable back to SongsScreen, so lets add a button in SearchScreen and expose a lambda function which will be called on that button clicked and navigation to SongsScreen will happen on higher level inside MainNavigation composable.

Lets look via code example, first see how new SearchScreen composable looks like

MainNavigation composable after implementing onNavigateToSongs lambda for SearchScreen

Now Run the App, you will see navigation working from HomeScreen -> SearchScreen -> SongsScreen.

BUT there is one problem! Once you are navigated to SongsScreen from SearchScreen and presses device back button you will see SearchScreen again, Normally and in most of the use-cases we don't want this intermediate screen to appear again but rather go back to HomeScreen

How to achieve this? You can achive this by implementing lambda function on navigate method of navController and poping everying up to home destination off the back stack before navigating to SongsSceen , see below code.

Updated code for MainNavigation composable

Run the App and navigate from HomeScreen -> SearchScreen -> SongsScreen, once you are on SongsScreen and pressing device back button will now show HomeScreen

There are some common use-cases in navigation let see how to achieve them

Use-case where you want to popUpTo route including that route: To achieve this you have to implement further lambda function of popUpTo method specifying { inclusive = true }

Use-case where navigating could lead to multiple copies: This normaly happens with Bottom Navigation or Drawer Navigaiton when user has always access to the tabs/selection and can tap multiple times on ui element which can lead to multiple entries for same screen in back stack of Navigation Controller, leading into bad behaviour while pressing device back button which will keep popping copies of the same screen.

How to avoid it? you should sepecify launchSingleTop = true in navigate method lambda function , see below code example

To summarise again on best pratice for Navigation.

Keep your navigation logic up in hierarchy exposing lambda functions for composables who want to navigate to other composables. It might overload composable function signatures but has benefits as explained below

There is a problem that route key strings are used multiple places, one in specifying destination composables in NavHost and others while navigating to particular destination, thats not good because its same key string used at multiple places and if you have to change you would need to change everywhere its specified and its gets harder and can introduce issues when code gets bigger

To improve this lets create a sealed class at top taking route as string key which keep track of all of our routes route keys

Now use them down in code

Thats all for navigation in this story BUT what about tests?

I have written another story about testing navigation component extending the same example here

Add a comment

Related posts:

The catcher

En el departamento de Mendoza y Freire compartíamos con Mati el cuarto de servicio. Si querías llegar hasta ahí tenías que atravesar, un poco agachado, el lavadero. El lavadero era un pasillo angosto…

Ketan Parekh Early life and Family

Ketan Parekh age is 60 years old and was born on December 18, 1966, in Mumbai, India. He came from a wealthy Gujarati family and was the youngest of three siblings. His father was a businessman who…

WELCOME TO THE GREAT KALIBABU HINDU TEMPLE.

The Great Kali Babu Hindu Temple of New York provides religious, spiritual, educational, social and cultural activities for all generations of the Hindu community. As the largest Hindu temple in New…