question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Weird back stack motion behavior on nested graph

See original GitHub issue

I have 4 navigation routes: first, second, third, fourth.

When i try to navigate first -> second -> third -> fourth, the transitions behave normally as it should.

However when i try to navigate back (back press) first <- second <- third <- fourth, the only transition that behave normally is the first pop backstack (in this case third <- fourth), other pop backstack just overlaps the current backstack.

I’m not sure how to explain it well but i have included a sample project for testing it here and a video here

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
uragiristereocommented, Apr 27, 2022

working perfectly fine, thank you for the release!

2reactions
fornewidcommented, Apr 26, 2022

I checked this issue. MaterialMotion is implemented based on AnimatedContent in Jetpack Compose Animation. And in AnimatedContent, I confirmed that the order of content is reversed when ‘pop’.

The followings are sample codes using accompanist AnimatedNavHost based on AnimatedContent.

import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.zIndex
import com.google.accompanist.navigation.animation.AnimatedNavHost
import com.google.accompanist.navigation.animation.composable
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
import soup.compose.material.motion.animation.materialElevationScaleIn
import soup.compose.material.motion.animation.materialElevationScaleOut

@OptIn(ExperimentalAnimationApi::class)
@Composable
fun NestedAnimatedNavigation() {
    val navHost = rememberAnimatedNavController()

    CompositionLocalProvider(LocalNavigation provides navHost) {
        AnimatedNavHost(
            navController = navHost,
            startDestination = "first",
        ) {
            composable(
                route = "first",
                enterTransition = {
                    when (initialState.destination.route) {
                        "second" -> slideInHorizontally { it }
                        else -> null
                    }
                },
                exitTransition = {
                    when (initialState.destination.route) {
                        "second" -> materialElevationScaleOut()
                        else -> null
                    }
                },
                popEnterTransition = {
                    when (initialState.destination.route) {
                        "second" -> materialElevationScaleIn()
                        else -> null
                    }
                },
                popExitTransition = {
                    when (initialState.destination.route) {
                        "second" -> slideOutHorizontally { it }
                        else -> null
                    }
                }
            ) {
                NextScreen(
                    title = "First",
                    color = Color.Cyan,
                    nextRoute = "second"
                )
            }

            composable(
                route = "second",
                enterTransition = {
                    when (initialState.destination.route) {
                        "first" -> slideInHorizontally { it }
                        "third" -> slideInHorizontally { it }
                        else -> null
                    }
                },
                exitTransition = {
                    when (targetState.destination.route) {
                        "first" -> materialElevationScaleOut()
                        "third" -> materialElevationScaleOut()
                        else -> null
                    }
                },
                popEnterTransition = {
                    when (initialState.destination.route) {
                        "first" -> materialElevationScaleIn()
                        "third" -> materialElevationScaleIn()
                        else -> null
                    }
                },
                popExitTransition = {
                    when (targetState.destination.route) {
                        "first" -> slideOutHorizontally { it }
                        "third" -> slideOutHorizontally { it }
                        else -> null
                    }
                }
            ) {
                NextScreen(
                    title = "Second",
                    color = Color.Red,
                    nextRoute = "third"
                )
            }

            composable(
                route = "third",
                enterTransition = {
                    when (initialState.destination.route) {
                        "second" -> slideInHorizontally { it }
                        "fourth" -> slideInHorizontally { it }
                        else -> null
                    }
                },
                exitTransition = {
                    when (targetState.destination.route) {
                        "second" -> materialElevationScaleOut()
                        "fourth" -> materialElevationScaleOut()
                        else -> null
                    }
                },
                popEnterTransition = {
                    when (initialState.destination.route) {
                        "second" -> materialElevationScaleIn()
                        "fourth" -> materialElevationScaleIn()
                        else -> null
                    }
                },
                popExitTransition = {
                    when (targetState.destination.route) {
                        "second" -> slideOutHorizontally { it }
                        "fourth" -> slideOutHorizontally { it }
                        else -> null
                    }
                }
            ) {
                NextScreen(
                    title = "Third",
                    color = Color.Green,
                    nextRoute = "fourth"
                )
            }

            composable(
                route = "fourth",
                enterTransition = {
                    when (initialState.destination.route) {
                        "third" -> slideInHorizontally { it }
                        else -> null
                    }
                },
                exitTransition = {
                    when (targetState.destination.route) {
                        "third" -> materialElevationScaleOut()
                        else -> null
                    }
                },
                popEnterTransition = {
                    when (initialState.destination.route) {
                        "third" -> materialElevationScaleIn()
                        else -> null
                    }
                },
                popExitTransition = {
                    when (targetState.destination.route) {
                        "third" -> slideOutHorizontally { it }
                        else -> null
                    }
                }
            ) {
                NextScreen(
                    title = "Fourth",
                    color = Color.Yellow
                )
            }
        }
    }
}

I found this issue before, and added the following code to fix it. https://github.com/fornewid/material-motion-compose/blob/main/core/src/main/java/soup/compose/material/motion/MaterialMotion.kt#L85

However, you have confirmed that this fix is not perfect either. So I have to think about new fixes.

    val forward: Boolean = pop.not()
    val density: Density = LocalDensity.current
+   val contentZIndex = remember { mutableStateOf(0f) }
    AnimatedContent(
        modifier = modifier,
        transitionSpec = {
            (motionSpec.enter.transition(forward, density) with motionSpec.exit.transition(forward, density))
                .apply {
                    // Show forward contents over the backward contents.
-                   targetContentZIndex = if (forward) 0.1f else 0f
+                   if (forward) {
+                       contentZIndex.value += 0.0001f
+                   } else {
+                       contentZIndex.value -= 0.0001f
+                   }
+                   targetContentZIndex = contentZIndex.value
                }
        },
        contentAlignment = contentAlignment,
    ) { currentState ->
        content(currentState)
    }

However, this is only a temporary patch. Finally, I should request to add a feature to AnimatedContent.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android Jetpack Navigation nested tab backward navigation ...
Yes, that back stack is expected (bottom nav always goes back through the startDestination of the graph, but doesn't go through intermediate ...
Read more >
Nested navigation graphs - Android Developers
The nested graph encapsulates its destinations. As with a root graph, a nested graph must have a destination identified as the start destination ......
Read more >
Stack is not restored for nested NavHostFragments [122770335]
My expected behavior is that the inner NavHostFragment stack is cleared by step 4 and the back button will navigate the user to...
Read more >
Motion User Guide (PDF) - Apple Support
Motion is a behavior-driven motion graphics application used to create stunning imaging effects in real time for a wide variety of broadcast, video, ......
Read more >
Using nested graph for a bottom bar destination recreates its ...
B is declared in a nested graph that is embedded in route so that some destination starting from B cannot be started from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found