Home > Net >  How to navigate back to the correct one screen before the previous in jetpack compose?
How to navigate back to the correct one screen before the previous in jetpack compose?

Time:09-03

I have four compose screens and by clicking on their items user leads to my AdShowScreen then after watching the ad they lead to the FinalShow screen. like the image below enter image description here

Now I want to navigate back to one of the first compose screens correctly by the back press button from FinalShowScreen. I mean from 3 to 1 in the image above. This is my navGraph.

@SuppressLint("UnrememberedMutableState")
@Composable
fun MyNavGraph(

navController: NavHostController) {
val actions = remember(navController) { MainActions(navController) }

NavHost(
    navController = navController,
    startDestination = BottomNavItems.First.route
 ) {
    composable(BottomNavItems.First.route) {
        FirstScreen(actions)
    }
    composable(BottomNavItems.Second.route) {
        SecondScreen(navController, actions)
    }
    composable(BottomNavItems.Third.route) {
        ThirdScreen()
    }
    composable(Screens.Fourth.route) {
        FourthScreen(navController, actions)
    }
         
    composable("${Screens.FinalShow.route}/{maskArg}") {
        val maskArg = it.arguments?.getString("maskArg")
        if (maskArg != null) {
            FinalShowScreen(
                maskArg = maskArg, navController,actions
            )
        }
    }
    
    
    composable("${Screens.RewardedShow.route}/{maskArg}") {
        val maskArg = it.arguments?.getString("maskArg")
        if (maskArg != null) {
            RewardedShowCompose(
                maskArg = maskArg, navController = navController, actions = actions
            )
        }
      }
   }
}

class MainActions(navController: NavController) {

val goWithArgument: (maskArg: String, route: String) -> Unit = { maskArg, route ->
    navController.navigate("$route/$maskArg") {
        launchSingleTop = true
        restoreState = true
    }
}


val goToRoute: (String) -> Unit = { route ->
    navController.navigate(route) {
        navController.graph.startDestinationRoute?.let { rout ->
            popUpTo(rout) {
                saveState = true
            }
        }
        launchSingleTop = true
        restoreState = true
    }
}
  

}

CodePudding user response:

You have to capture the back press in your finalshowscreen and then popUpTo like it says in the docs with this snippet:

// Pop everything up to and including the "home" destination off
// the back stack before navigating to the "friendslist" destination
navController.navigate("friendslist") {
    popUpTo("ad_show_compose_screen") { inclusive = true }
}

so you pop the adshow screen and then you end up on the screen whichever was opened 1 or 2 or 3 or 4

CodePudding user response:

To navigate back from the final screen to the first one, add this to the FourthScreen Composable;

BackHandler {
  navController.navigate(BottomNavItems.First.route) // Or to whichever route you want to navigate to
}

Also, it is not a good practice to pass the navController to other Composables because it will make your UI pretty hard to test.

  • Related