Getting an issue in compose navigation which was w...
# compose
c
Getting an issue in compose navigation which was working fine in
2.4.0-alpha04
Whenever I do
navController.popBackStack()
, I see the behavior attached in the video. For now the fix is by downgrading the navigation version.
Copy code
"androidx.navigation:navigation-compose:2.4.0-alpha04"            // Working fine

vs

"androidx.navigation:navigation-compose:2.4.0-alpha05" or above   // Doesn't work
Is there any way to fix it up or I have to wait until it is being fixed by Google?
This behavior is found till 2.5.0-alpha03
Looks like this commit caused the issue:
https://android-review.googlesource.com/c/platform/frameworks/support/+/1750073
i
Where exactly are you calling
popBackStack()
?
You can file an issue with a sample project that reproduces your issue at https://issuetracker.google.com/issues/new?component=409828&template=1093757
c
My mistake, on click of
ACTION_UP
, I call
onBackPressed
Copy code
val dis = LocalOnBackPressedDispatcherOwner.current?.onBackPressedDispatcher

Scaffold(
    topBar = {
        AppBar {
            BackMenu {
                dis?.onBackPressed()
            }
        }
    },
    modifier = Modifier.fillMaxSize()
) {
}
By the way, this issue persists even for
popBackStack()
Copy code
val dis = LocalOnBackPressedDispatcherOwner.current?.onBackPressedDispatcher

Scaffold(
    topBar = {
        AppBar {
            BackMenu {
                navController.popBackStack()
            }
        }
    },
    modifier = Modifier.fillMaxSize()
) {
}
@Ian Lake Here is the issue with attached screen record and code to reproduce. https://issuetracker.google.com/issues/225987040
i
Oh, it is just multiple recompositions? That's always going to be the case when there's animations (like the crossfade that NavHost uses) - that's just how animations work in Compose
c
No, it's not only the multiple recomposition. If you see the code I submitted doesn't have any animation. Moreover, what changed from
alpha04
and
alpha05
which caused this recomposition? There is another problem. If I have a stack
A -> B -> C
and by doing
navController.popBackStack()
, on few scenarios, I see it comes back to
A
instead of
B
i
Every NavHost animates between every destination as of alpha05, that's what is specifically called out in the release notes: https://developer.android.com/jetpack/androidx/releases/navigation#2.4.0-alpha05
If you aren't guarding your popBackStack calls with a check for whether the Lifecycle is RESUMED (i.e., you aren't animating out), then it sounds like you are just double tapping the button
c
Double tapping the button? Why should I do that? Can you please check the snippet or the project submitted to reproduce and let me know what's wrong there?
I don't double tap actually
i
I guess logging your click listeners would be the best way to find out. Like I said, your code is allowing users to tap the buttons even after you've started navigating to another screen. Checking the Lifecycle state of the NavBackStackEntry you've been passed is the 100% foolproof way to ignore those click events during the crossfade
c
Tried adding a log on the click listener and it prints only ones in the logcat
Copy code
Button(
    onClick = {
        println("Details Screen Clicked")
        onClick()
    }
) {
    Text("Go Back")
}
I/System.out: Details Screen Clicked I/System.out: List Screen I/System.out: List Screen I/System.out: List Screen I/System.out: List Screen
On the other hand, if I do it to
alpha04
, the log prints as I/System.out: Details Screen Clicked I/System.out: List Screen
i
Yep, that looks like the recompositions caused by crossfade and is working as intended with how Compose animations work
I was referring to your completely separate issue of sometimes having it pop back two steps - that's the case where you've just triggered your click twice (Navigation is just doing exactly what you told it to do)
You should never, ever be starting side effects as part of composition; it should be completely fine to recompose thousands of time. As long as your state isn't changing, none of the compositions under the level that is animating is actually doing any additional work
c
So, are you saying the blink in the recorded video is happening because of side effects? If yes, I will dig into the code to find ou.