Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing URL as a parameter to Jetpack Compose Navigation

I have created a destination for HistoryDetail screen in my app.

composable(
    route = "HistoryDetail/{webpage}",
    arguments = listOf(
        navArgument("webpage") {
            type = NavType.StringType
        }
    ),
) { entry ->
    val text = entry.arguments?.getString("webpage") ?: ""
}

When I try to navigate to that screen by calling:

navController.navigate("HistoryDetail/http://alphaone.me/")

I'm getting illegalArgumentException with the below message.

java.lang.IllegalArgumentException: Navigation destination that matches request NavDeepLinkRequest{ uri=android-app://androidx.navigation/HistoryDetail/http://alphaone.me/ } cannot be found in the navigation graph NavGraph(0x0) startDestination={Destination(0x78c9ba0c) route=Home}

Edit:

It works if I call: navController.navigate("HistoryDetail/test").

like image 382
Alpha 1 Avatar asked Aug 27 '21 09:08

Alpha 1


People also ask

How do I pass arguments to a route in jetpack compose?

The way it is currently done in jetpack compose is by appending the route with arguments and their respective values. For example, let’s say that we have a composable with route = “userPage”, and we want to pass arguments “userId” and “isLoggedIn”. The following snippets show how to do that in jetpack compose.

Can I Pass parcelable in the navigation composable routes?

NOTE: The Jetpack Compose team doesn’t recommend passing Parcelable in the navigation composable routes.

Is it possible to pass parcelable objects as arguments to destinations?

In the view system navigation component, we were able to pass parcelable objects as arguments to the destinations, you might be tempted to do that in Compose Navigation as well and write up code that looks like this:

How do I use compose with Android navcontroller?

To support Compose, use the following dependency in your app module’s build.gradle file: The NavController is the central API for the Navigation component. It is stateful and keeps track of the back stack of composables that make up the screens in your app and the state of each screen.


1 Answers

Navigation routes are equivalent to urls. Generally you're supposed to pass something like id there.

When you need to pass a url inside another url, you need to encode it:

val encodedUrl = URLEncoder.encode("http://alphaone.me/", StandardCharsets.UTF_8.toString())
navController.navigate("HistoryDetail/$encodedUrl")
like image 131
Philip Dukhov Avatar answered Oct 04 '22 15:10

Philip Dukhov