prudhvir3ddy
prudhvir3ddy

Reputation: 1036

Jetpack compose navigation with multiple optional argments

https://developer.android.com/jetpack/compose/navigation#optional-args

been going through documentation to figure out how to use multiple optional arguments and how to pass them

but in docs only one param is mentioned.

composable(
    "profile?userId={userId}",
    arguments = listOf(navArgument("userId") { defaultValue = "me" })
) 

and call it with

composable("profile")
composable("profile/user123") // if you want to pass param

how to declare and call for two params ?

Upvotes: 16

Views: 15514

Answers (3)

Soft Code
Soft Code

Reputation: 57

#Extension Function

 import android.os.Bundle
    import androidx.core.net.toUri
    import androidx.navigation.*
    
    fun NavController.navigate(
        route: String,
        args: Bundle,
        navOptions: NavOptions? = null,
        navigatorExtras: Navigator.Extras? = null
    ) {
        val routeLink = NavDeepLinkRequest
            .Builder
            .fromUri(NavDestination.createRoute(route).toUri())
            .build()
    
        val deepLinkMatch = graph.matchDeepLink(routeLink)
        if (deepLinkMatch != null) {
            val destination = deepLinkMatch.destination
            val id = destination.id
            navigate(id, args, navOptions, navigatorExtras)
        } else {
            navigate(route, navOptions, navigatorExtras)
        }
    }

#Send

 val args = Bundle().apply {
                putString("emailId", emailId)
                putString("password", password)
            }
            navController.navigateWithArgs(
                route = NavRoute.LoginScreen.route, args = args
            )

#Receive

navGraphBuilder.composable(
        route = NavRoute.LoginScreen.route
    ) { navBackStackEntry ->
        val args = navBackStackEntry.arguments
        val emailId: String = args?.getString("emailId").toString()
        val password: String = args?.getString("password").toString()
     
    }

Dulicate question of Pass Parcelable argument with compose navigation

Upvotes: -1

Sriyank Siddhartha
Sriyank Siddhartha

Reputation: 1982

Here is a detailed answer for using multiple optional parameters

// Syntax to declare 
NavHost(...) {
    composable(
        "profile?userId={userId}&username={username}&address={address}",
        arguments = listOf(
            navArgument("userId") { 
                type = NavType.IntType  
                defaultValue = 86 
            } 
            navArgument("username") { 
                type = NavType.StringType  
                defaultValue = "rahul2211" 
            } 
            navArgument("address") { 
                type = NavType.StringType  
                defaultValue = "India" 
            } 
        ) 
    ) { backStackEntry -> 
        val userId = backStackEntry.arguments?.getInt("userId") 
        val username = backStackEntry.arguments?.getString("username") 
        val address = backStackEntry.arguments?.getString("address") 
        Profile(navController, userId, username, address) 
    } 
}

This is how you can optionally pass parameters to the Profile() composable

// Syntax to call 

navController.navigate("profile?userId=102&username=shrekssid&address=Germany")     
// userId = 102, username = "shrekssid", address = "Germany"

navController.navigate("profile?address=Italy")                                     
// userId = 86, username = "rahul2211", address = "Italy"

navController.navigate("profile?userId=991&address=Germany")                        
// userId = 991, username = "rahul2211", address = "Germany"

navController.navigate("profile")                                                   
// userId = 86, username = "rahul2211", address = "India" 

Upvotes: 12

prudhvir3ddy
prudhvir3ddy

Reputation: 1036

how to declare ?

  composable(
    "profile?userId={userId}&userType={userType}",
    arguments = listOf(
      navArgument("userType") {
        defaultValue = "ADMIN"
        type = NavType.StringType
      }, navArgument("userId") {
        nullable = true
        defaultValue = null
        type = NavType.StringType
      })
  ) 

how to call ?

navController.navigate("profile?userId=user123&userType=user")
navController.navigate("profile?userType=user")
navController.navigate("profile")

Upvotes: 59

Related Questions