Blog Infos
Author
Published
Topics
, , ,
Published
implementation "androidx.navigation:navigation-compose:$compose_navigation_version"

The setup of a navigation graph in Jetpack Compose is slightly different from the traditional setup found within a non-Compose app, for example:

@AndroidEntryPoint
class MainActivity: AppCompatActivity() {
...
   @Composable
   fun MyApp() {
       // setup the navController this way
       val navController = rememberNavController()
   NavHost(
       navController, // the navController created above
       startDestination = "dashboard" // start destination variable needs to match one of the composable screen routes below
   ) {
       composable("dashboard") { DashboardScreen(navController) }
       composable("library") { LibraryScreen(navController) }
     }
   }

where the NavHost expects two main parameters — the NavController and the start destination to be defined; and all the app’s composables (or screens) to be added to the navigation graph through the use of NavGraphBuilder.composable:

composable("dashboard") { DashboardScreen(navController) }
@Composable
fun DashboardScreen(navController: NavController) {
    val viewModel: DashboardViewModel = viewModel()
}

 

Job Offers

Job Offers


    Mobile Developer Android (m/w/d) Firebase

    REWE digital
    Köln / Cologne
    • Full Time
    apply now

    Softwareentwickler Java und Kotlin / Android (w/m/d)

    AVM GmbH
    Berlin
    • Full Time
    apply now

    Senior Android Engineer

    Busuu
    Madrid
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

Jobs

java.lang.RuntimeException: Cannot create an instance of class com.exampleapp.ui.dashboard.DashboardViewModel 
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278)
        at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:112)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
        at androidx.lifecycle.viewmodel.compose.ViewModelKt.get(ViewModel.kt:87)
        at androidx.lifecycle.viewmodel.compose.ViewModelKt.viewModel(ViewModel.kt:72)
        at com.exampleapp.ui.dashboard.DashboardScreenKt.DashboardScreen(DashboardScreen.kt:17)
        at com.exampleapp.MainActivity$ExampleApp$2$1$2.invoke(MainActivity.kt:156)
        at com.exampleapp.MainActivity$ExampleApp$2$1$2.invoke(MainActivity.kt:156)
implementation "androidx.hilt:hilt-navigation-compose:$compose_hilt_navigation_version"
@Composable
fun DashboardScreen(navController: NavController) {
    val dashboardViewModel = hiltViewModel<DashboardViewModel>()
}

and the aforementioned crash was resolved.

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s been almost a year since I wrote an article on how I handle…
READ MORE
blog
As part of a recent project, I decided to utilize Jetpack Compose for my…
READ MORE
blog
State hoisting is an important concept useful to make a Composable stateless (and so…
READ MORE

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu