david.bilik
08/17/2021, 12:51 PMnavigation-compose
with viewmodels scoped to NavBackStackEntry
to pass complex parcelable arguments since it’s not possible natively from list to detail screen. It works fine but when I press button it looks like the detail screen is “requested” again in composable
call but the previous back stack entry is null. Details in 🧵Colton Idle
08/17/2021, 1:34 PMdavid.bilik
08/17/2021, 1:44 PMval navArgVM = viewModel<StreamDetailNavViewModel>rootNavController.currentBackStackEntry!!)
Card(
modifier = Modifier
.clickable {
navArgVM.setStreamUri(uiStream.stream.url.toString())
rootNavController.navigate(
Destination.StreamDetail.route
)
},
)
and when I’m reading it when navigating I have something like this
NavHost(rootNavController, startDestination = Destination.Main.route) {
composable(Destination.StreamDetail) {
rootNavController.previousBackStackEntry?.let { prevBackstackEntry ->
val navArgVM = viewModel<StreamDetailNavViewModel>(prevBackstackEntry)
StreamDetailScreen(navArgVM.getStreamUri())
}
}
...
my problem is that this composable
is called on back press click and in that situation previousBackStackEntry
is null which will for a brief momenty display my windowBackground
before the list is displayed again (hence I don’t emit any Composable in that case). Is this even the correct way how to tie viewmodel to the backstack entry?Ian Lake
08/17/2021, 1:49 PMrootNavController.navigate(
Destination.StreamDetail.route + "/" +
Uri.encode(uiStream.stream.url.toString())
)
Ian Lake
08/17/2021, 1:52 PMdavid.bilik
08/17/2021, 1:55 PMIan Lake
08/17/2021, 2:13 PMIan Lake
08/17/2021, 2:14 PMIan Lake
08/17/2021, 2:14 PMdavid.bilik
08/17/2021, 2:21 PMBundle
) of nav stack entries. My app is small, apx. 5 screens, but I’ve passed around NavArgs
classes which were parcelables. Now I’m migrating this app to compose 1.0.1
and my previous approach is not working so I’m trying to find a way how not to rewrite all of the arguments to query strings because it would require more work than this approach which I’ve found in this discussionColton Idle
08/17/2021, 2:34 PMdavid.bilik
08/17/2021, 2:36 PMShakil Karim
08/17/2021, 2:38 PMColton Idle
08/17/2021, 2:39 PMColton Idle
08/17/2021, 2:40 PMColton Idle
08/17/2021, 2:41 PMdavid.bilik
08/17/2021, 2:43 PMShakil Karim
08/17/2021, 2:45 PMColton Idle
08/17/2021, 2:46 PMdavid.bilik
08/17/2021, 2:56 PMnav scoped view modelsis exactly what I was trying to achieve but probably in a wrong way
Ian Lake
08/17/2021, 4:55 PMIan Lake
08/17/2021, 4:56 PMdavid.bilik
08/18/2021, 5:42 AMIan Lake
08/18/2021, 5:28 PMdavid.bilik
08/19/2021, 5:50 AM