Predicitve back with Compose navigation
Asked Answered
V

1

7

According to the Android Developer docs it is possible to implement custom animations for the predictive back gesture. I got it to work on an example similar to the one they present in their docs with the Transition APIs.

However, I would like to implement a similar gradular transition based on the back progress for the navigation component in Jetpack Compose.

How would I do so? I can't find any documentation on being able to control the progress of the transition between different navigation destinations.

Here's an example of my NavHost:

NavHost(navController = pageNavController!!, startDestination = "home"){
    composable(
    route = "home",
    enterTransition = { 
        navEnterTransition(
            direction = DIRECTION_LEFT, 
            orientation = orientation) },
    exitTransition = { 
        navExitTransition(
            direction = DIRECTION_LEFT, 
            orientation = orientation) },
        content = { HomePage() }
    )
    composable(
        route = "settings",
        enterTransition = { navEnterTransition(
            direction = getNavEnterDirection(initialState.destination),
            orientation = orientation) },
        exitTransition = { navExitTransition(
            direction = getNavExitDirection(initialState.destination),
            orientation = orientation) },
        content = { SettingsPage() }
    )
}

Is there a way to implement the custom predictive back gestures with this navigation approach?

If not, are there other approaches that might work?


Edit

As ianhanniballake pointed out, this is not supported yet.

However, I was able to somewhat get it to work using the Pager and some complicated custom navigation and modifiers. This worked and incorporated the predictive back gesture but was very unstable and had some bugs so I decided against and I will just wait for official support.

Valer answered 10/10, 2023 at 11:56 Comment(2)
This isn't supported yet, but will be automatic in the future. You can track progress in this issue.Paschall
for compose components, seems only 4 components support this. Bottom Sheet, Side Sheet, Navigation Drawer and Search. developer.android.com/guide/navigation/custom-back/…Angeliqueangelis
T
1

To enable predictive back set android:enableOnBackInvokedCallback="true" in your root Activity in the AndroidManifest.xml, and make use of Navigiation at least 2.8.X. You can find the latest version here.

As mentioned by @NanoNova all components might not have been migrated yet, but you can read the full list here

This blog post from Google cover the roadmap of migrating to Predictive Back pretty well.

Current state: I've experimented with 2.8.0-beta02 which still had some bugs, but some of those have been addressed in beta04.

Thirtytwomo answered 3/7, 2024 at 14:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.