Sparsh Dutta
Sparsh Dutta

Reputation: 3008

Popping off screen (from "back stack") represented by a composable to go back to previous screen while using navigation component of Jetpack Compose?

I am creating a single activity app with Jetpack compose using it's navigation component. I have created composables to represent different screens, and used the navigation component as:

@InternalCoroutinesApi
@ExperimentalAnimationApi
@ExperimentalPagerApi
@RequiresApi(Build.VERSION_CODES.M)
@ExperimentalComposeUiApi
@Composable
fun ProgressNavigation(themeViewModel: ThemeViewModel?) {

    val navController = rememberNavController()
    NavHost(
        navController = navController,
        startDestination = Constants.SPLASH
    ) {composable(Constants.SPLASH) {
        ScreenAnimation {
            Splash(navController = navController)
        }
    }
        composable(Constants.HIW) {
            ScreenAnimation {
                LogoPage(false, true, navController = navController) { HowItWorks( navController = navController) }
            }
        }
.
.
.
}

Let's say I have 2 screens (composables) "SignUp" and "TnC". User enters the sign up details in SignUp screen then goes to TnC screen. On coming back to SignUp screen by pressing a button in TnC screen the details entered earlier in SignUp should remain intact. How do I implement this? If the screens were activities I would do it by popping off the screen from backstack using onBackPressed. How do I do this in my case?

Upvotes: 3

Views: 6617

Answers (1)

Sandro Kakhetelidze
Sandro Kakhetelidze

Reputation: 409

Replace by remember with by rememberSaveable in SingUp composable, then you can use navController.popBackStack() if you are just navigating back or you can use popUpTo. if you want to navigate back further.

example:

navController.popBackStack()
//or
navController.navigate("TnC") {
    popUpTo("SignUp")
}

Upvotes: 6

Related Questions