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


    Developer (m/w/d) Backend/ Mobile

    Payback GmbH
    Cologne, Germany
    • Full Time
    apply now

    Android Engineering Manager

    Peloton
    New York, USA
    • Full Time
    apply now

    Sr. Software Engineer, Android

    Box
    Redwood City, California; USA
    • 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 one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog

How to animate BottomSheet content using Jetpack Compose

Early this year I started a new pet project for listening to random radio…
READ MORE
blog
I recently found a bug that would cause a crash in all the apps…
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