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


    Android Software Engineer (f/m/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now

    Android Test Automation Engineer

    Komoot
    Remote
    • Full Time
    apply now

    Senior Android Software Engineer (f/m/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now

OUR VIDEO RECOMMENDATION

, ,

Testing: how hard can it be?

When people start looking into the testing domain, very similar questions arise: What to test? And more important, what not? What should I mock? What should I test with unit tests and what with Instrumentation?…
Watch Video

Testing: how hard can it be?

DANNY PREUSSLER
Android Lead
Soundcloud

Testing: how hard can it be?

DANNY PREUSSLER
Android Lead
Soundcloud

Testing: how hard can it be?

DANNY PREUSSLER
Android Lead
Soundcloud

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. Required fields are marked *

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

Menu