I'm using Jetpack Compose navigation in an app wit...
# compose
j
I'm using Jetpack Compose navigation in an app with multiple activities, each with their own separate nav graph (multiple activities is a requirement). My workflow is to navigate from Activity 0 -> Activity 1 -> Activity2 then navigate back to Activity 0 and clear the back stack. I can't find a way to clear the back stack when navigating back to Activity 0. Since I'm using NavGraphBuilder, I don't have direct access to the Intent used to launch the activity. Through
ActivityNavigatorDestinationBuilder
, I'm able to access the action, data, and dataPattern used to create the intent, but I don't see a way to let me set flags like
FLAG_ACTIVITY_NEW_TASK
or
FLAG_ACTIVITY_CLEAR_TOP
. How can I navigate back to Activity 0 and clear the back stack?
Here is what the NavGraphBuilder currently looks like for Activity 2:
Copy code
fun NavGraphBuilder.generateNavGraphFlow2(navController: NavHostController) {
    composable(NavRoutes.Flow2Screen6.route) {
        Flow2Screen6(
            onNavigateToFlow2Screen7 = { navController.navigate(NavRoutes.Flow2Screen7.route) }
        )
    }
    composable(NavRoutes.Flow2Screen7.route) {
        Flow2Screen7(
            onNavigateToHome = {
                navController.navigate(NavRoutes.Home.route)
            }
        )
    }
    activity(NavRoutes.Home.route) {
        this.activityClass = ActivityZero::class
    }
}
i
Custom flags are passed as part of an
ActivityNavigator.Extras
instance: https://developer.android.com/reference/androidx/navigation/ActivityNavigator.Extras
Which gets you something like:
Copy code
navController.navigate(
  NavRoutes.Home.route,
  null, // no NavOptions
  ActivityNavigatorExtras(flags= Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK))
)
Which gets you something like:
Copy code
navController.navigate(
  NavRoutes.Home.route,
  null, // no NavOptions
  ActivityNavigatorExtras(
)
j
Thanks!
557 Views