Hello, I'm trying to pass a state to the navigation for me to use in the screen, i tried just passin...
f
Hello, I'm trying to pass a state to the navigation for me to use in the screen, i tried just passing the location to the navhost but it does not update when the variable does so i found it out about this hilt thing, but i'm getting a runtime error..
๐Ÿงต 2
c
@Felipe Passos can you edit your post to put the code in this thread please?
f
Oh, okay
๐Ÿ™ 2
I'm trying to pass a state to the navigation for me to use in the screen, i tried just passing the location to the navhost but it does not update when the variable does so i found it out about this hilt thing, so i added the dependencies on the main build file
Copy code
dependencies {
        classpath 'com.android.tools.build:gradle:7.0.0-alpha14'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31"
        classpath "com.google.dagger:hilt-android-gradle-plugin:2.35"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
and in the app build file:
Copy code
implementation "com.google.dagger:hilt-android:2.35"
    implementation "androidx.hilt:hilt-navigation-compose:1.0.0-alpha01"
Added this line to the main file:
Copy code
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    private val _tag = "TAXI"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
and on the navHost :
Copy code
@Composable
fun SearchNavigator(location: String) {
    val navController = rememberNavController()
    Log.d("LOCATION_IN_NAV", location)
    Column(
        modifier = Modifier
            .background(white)
            .padding(vertical = 10.dp)
            .fillMaxHeight()
            .fillMaxWidth()
    ) {

        NavHost(navController, startDestination = "MainSearch") {
            composable("MainSearch") { backStackEntry ->
                val viewModel = hiltNavGraphViewModel<LocationViewModel>(backStackEntry = backStackEntry)
                Log.d("LOCATION_IN_SEARCH", viewModel.name.hasObservers().toString())

                Form(location, navController)
But i'm getting a runtime error saying that the AndroidEntryPoint is not setted :
Copy code
2021-04-27 07:52:57.310 10438-10438/com.gotootaxi E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.gotootaxi, PID: 10438
    java.lang.IllegalStateException: Given component holder class com.gotootaxi.MainActivity does not implement interface dagger.hilt.internal.GeneratedComponent or interface dagger.hilt.internal.GeneratedComponentManager
        at dagger.hilt.EntryPoints.get(EntryPoints.java:62)
What i'm doing wrong?
a
Skipping the hilt and ViewModel stuff entirely, I think there's still an outstanding issue where the nav graph built by the NavHost composable does not update for recompositions, which in this case means the new lambda capture for a new parameter value of
location
won't be applied.
You can work around this by doing
val currentLocation by rememberUpdatedState(location)
as the first line of the SearchNavigator function, then use
currentLocation
instead of
location
everywhere else in your snippet.
cc @Jeremy Woods
f
Wow, that worked
I was trying to found something to make this work and could not find it and this simple solution worked, thank you very much!
๐Ÿ‘ 1
a
in your defense, the reasons the original code didn't work and why the workaround does are not simple ๐Ÿ™‚ I think your original expectations were correct and our code isn't meeting those expectations today
๐Ÿ™‚ 1
j
Yeah, we are taking a closer look at NavGraph in base navigation to see how best to handle this case. I think it could be a little more dynamic in general so these types of use cases just work.