Tapan Desai
07/08/2025, 4:08 PMNavigation3
library right now.
I have come across a scenario where we need to scope a ViewModel
to the previous screen
Current we are using compose-destinations which is built on top of Jetpack Navigation library
We scope a ViewModel
to the previous destination like this. Can this is be achieved in Navigation3
?
@Composable
@Destination(style = DestinationStyleBottomSheet::class)
fun ColumnScope.SalesStaffBottomSheet(
navigator: DestinationsNavigator,
navBackStackEntry: NavBackStackEntry,
navController: NavController,
result: ResultBackNavigator<SalesStaffDetails>,
) {
val backStack = remember(navBackStackEntry) {
navController.getBackStackEntry(AnalyticsMobileDestination.route)
}
val viewModel = hiltViewModel<AnalyticsViewModel>(backStack)
val uiState by viewModel.analyticsUiState.collectAsStateWithLifecycle()
SalesStaffDialogBottomSheet(
uiState = uiState,
onSalesStaffClick = { result.navigateBack(it) },
onCloseClick = { navigator.popBackStack() }
)
}