Hussein Chahine
Hussein Chahine

Reputation: 1

Viewmodel re initialization while its not cleared after navigating between screens

This is my bottom app bar : ShoppingCartPage-HomePage-ProfilePage-MyOrders-SettingsPage

And Two screens: CategoryItemPage - OfferItemPage ( i dont want to save their states)

navigation: HomePage -> CategoryItemPage - > ShoppingCart to add items in shoppingCart

and HomePage -> OfferItemPage -> ShoppingCart to add items too

fun NavHostController.navigateSingleTopTo(
    route: CurrentDestination,
    currentDestinationRoute: String
) = this.navigate(route) {
    // If the destination doesn't exist, pop up to the start destination
    popUpTo(currentDestinationRoute) {
        //inclusive = true this means in the backstack entry the old destination will be replaced by the new destination
        //inclusive = false this means the backstack entry will put the new destination at the top and below it will keep the old destination
        inclusive = true
        saveState = true
    }
    launchSingleTop = true
    restoreState = true
}

  FloatingButtonBar(onShoppingCartClick = {
                        navController.navigateSingleTopTo(
                            route = CurrentDestination.ShoppingCartPage(),
                            currentDestinationRoute = currentScreenDestination.toString()
                        )
                    })
                }
 BottomAppBar(
                                onHomeClick = {
                                    navController.navigateSingleTopTo(
                                        route = CurrentDestination.HomePage,
                                        currentDestinationRoute = currentScreenDestination.toString()
                                    )
                                },
                                onProfileClick = {
                                    navController.navigateSingleTopTo(
                                        route = CurrentDestination.ProfilePage,
                                        currentDestinationRoute = currentScreenDestination.toString()
                                    )
                                },
                                onMyOrdersClick = {
                                    navController.navigateSingleTopTo(
                                        route = CurrentDestination.MyOrders,
                                        currentDestinationRoute = currentScreenDestination.toString()
                                    )
                                },
                                onSettingsClick = {
                                    navController.navigateSingleTopTo(
                                        route = CurrentDestination.SettingsPage,
                                        currentDestinationRoute = currentScreenDestination.toString()
                                    )
                                }

HomePage:

 onItemClick = { categoryItems ->
                    navController.navigate(
                        CurrentDestination.CategoryItemPage(categoryItems = categoryItems)
                    ){
                    popupto(CurrentDestination.HomePage){
                    inclusive = true
                    saveState = true
                        }
                    restoreState = true
                    launchedSingleTop = true
                },
                onOffersClick = { offers ->
                    navController.navigate(
                        CurrentDestination.OfferItemPage(offers = offers)
                    ){
                        popupto(CurrentDestination.HomePage){
                        inclusive = true
                        saveState = true
                            }
                        restoreState = true
                        launchedSingleTop = true
                        }
                })

CategoryItemPage:

 addToCard = { categoryItemCart ->
                    navController.navigate(
                        CurrentDestination.ShoppingCartPage(
                            categoryItemsCart = categoryItemCart, offerCart = OfferCart()
                        )
                    ) {
                        popUpTo(route = currentRouteDestination) {
                            inclusive = true
                        }
                    }

OfferItemPage:

navController.navigate(
                        CurrentDestination.ShoppingCartPage(
                            categoryItemsCart = CategoryItemsCart(), offerCart = offerCart
                        )
                    ) {
                        popUpTo(route = currentRouteDestination) {
                            inclusive = true
                        }

Here when i navigate using Bottom app Bar button ShoppingCart its re initializing its viewmodel and list showing empty. Im still learning the popupto not able to get exact one here to achieve my goal.

Appreciate any help!

Upvotes: 0

Views: 16

Answers (0)

Related Questions