How to pass id and application to a viewModel/viewModelFactory in Jetpack Compose?

To answer your question: you retrieve the Application from the LocalContext object: val context = LocalContext.current val application = context.applicationContext as Application However, when using Navigation Compose, you don’t need to manually pass any arguments to your ViewModel. Instead, you can utilize the built in support for SavedState in ViewModels and add a SavedStateHandle parameter … Read more

Pass Parcelable argument with compose navigation

Warning: Ian Lake is an Android Developer Advocate and he says in this answer that pass complex data structures is an anti-pattern (referring the documentation). He works on this library, so he has authority on this. Use the approach below by your own. Edit: Updated to Compose Navigation 2.4.0-beta07 Seems like previous solution is not … Read more

Why the view keeps flashing when using jetpack navigation with Compose?

Composite navigation recomposes both disappearing and appearing views during transition. This is the expected behavior. You’re calling navigate on each recomposition. Your problem lays in these lines: if (viewModel.isLoginSuccessful) { navController.navigate(Screen.AccountsScreen.route) { popUpTo(Screen.LoginScreen.route) { inclusive = true } } } You shouldn’t change state directly from view builders. In this case LaunchedEffect should be used: … Read more

Fragments destroyed / recreated with Jetpack’s Android Navigation components

Ian Lake from google replied me that we can store the view in a variable and instead of inflating a new layout, just return the instance of pre-stored view on onCreateView() Source: https://twitter.com/ianhlake/status/1103522856535638016 Leakcanary may show this as leak but its false positive..

IllegalArgumentException: navigation destination xxx is unknown to this NavController

In my case, if the user clicks the same view twice very very quickly, this crash will occur. So you need to implement some sort of logic to prevent multiple quick clicks… Which is very annoying, but it appears to be necessary. You can read up more on preventing this here: Android Preventing Double Click … Read more