question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dependencies to composable and access to DestinationDependenciesContainer

See original GitHub issue

Before, in version 0.9.2, we could provide a DestinationDependenciesContainer to a DestinationsNavHost to provide some rudimentary dependency injection to Destinations.

It seems like this feature is no longer accessible unless I provide my own NavHostEngine due to the default enigine is creating a empty DestinationDependenciesContainer by default. Is this a conscious design decision? I looked into using the manual compose calls but that easily becomes quite messy.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Rawacommented, Jan 26, 2022

Sounds, good! Thank you for the help and the nice library.

2reactions
raamcostacommented, Jan 25, 2022

I see. At the moment, I think CompositionLocal is the correct solution for you then.

// top level
val LocalNavigator = compositionLocalOf<Navigator> { error("No LocalNavigator was provided") }

// inside your Activities or every time you need to provide a different Navigator instance
CompositionLocalProvider(LocalNavigator provides myNavigator) {
    // ... Content goes here , for example your DestinationsNavHost call
    // This part of Composition will see the `myNavigator` instance
    // when accessing LocalNavigator.current
}

// then in your screens:
@Destination
@Composable
fun MyScreen(
    navigator: Navigator = LocalNavigator.current
) {
    navigator.navigate//...
}

This specific code was not tested, so take it with a grain of salt.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Compose and other libraries - Android Developers
To use Compose in an activity, you must use ComponentActivity ... you can access a ViewModel from any composable by calling the viewModel()...
Read more >
Navigating through multi-module Jetpack Compose applications
Jetpack Compose, navigation, multi-module architecture, API and Impl modules, dependency injection — in this blog post, we will see how ...
Read more >
Jetpack Compose How to access dependency injected view ...
In fragment /activity, you should wrap composable view with CompositionLocalProvider and provide the viewmodel instance to the view tree.
Read more >
Navigation and Dependency Injection in Compose - Droidcon
Applying the lessons Square has learned about DI-friendly navigation patterns to Compose-first apps. Life gets better when you recognize ...
Read more >
False positive "Cannot access class 'androidx.compose.ui ...
Cannot access class 'androidx.compose.ui.geometry.Offset'. Check your module classpath for missing or conflicting dependencies. floating in drawCricle() .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found