Joost Klitsie
09/18/2020, 9:26 AM<MainNavGraph>
<StartFragment>
<LoginFlowGraph>
<UsernameFragment>
<PasswordFragment>
</LoginFlowGraph>
<LoggedInGraph>
<HomeFragment>
</LoggedInGraph>
</MainNavGraph>
My problem is this: when I am logged in, I want to go to the LoggedInGraph and pop everything what is on the backstack:
if (!navController.currentDestination.isPartOfGraph(R.id.loggedInNavGraph)) {
navController.popBackStack(R.id.mainNavGraph, false)
navController.navigate(R.id.loggedInNavGraph)
}
This works fine. The graph is basically emptied to the root and then the loggedInNavGraph starts.
I want to use material transitions during this, On the login flow I use Material shared transition on the X axis
enterTransition = MaterialSharedAxis(MaterialSharedAxis.X, true)
returnTransition = MaterialSharedAxis(MaterialSharedAxis.X, false)
On the HomeFragment I use transitions on the Z axis:
enterTransition = MaterialSharedAxis(MaterialSharedAxis.Z, true)
returnTransition = MaterialSharedAxis(MaterialSharedAxis.Z, false)
However, popping the backstack after the login flow, shows the fragments in the backstack also with the wrong animation... Is it possible to clear the backstack without the fragments from within the backstack to show themselves during the transition?Joost Klitsie
09/18/2020, 9:30 AMJoost Klitsie
09/18/2020, 9:31 AMJoost Klitsie
09/18/2020, 12:32 PMJoost Klitsie
09/18/2020, 12:33 PMclearAnimationsForNavBackStack()
navController.popBackStack(R.id.mainNavGraph, true)
navController.navigate(R.id.loggedInNavGraph)
private fun clearAnimationsForNavBackStack() {
val navigationFragmentManager = navigationFragmentManager ?: return
if (navigationFragmentManager.backStackEntryCount > 0) {
navigationFragmentManager.addOnBackStackChangedListener(object : FragmentManager.OnBackStackChangedListener {
override fun onBackStackChanged() {
currentNavigationFragment?.apply {
enterTransition = null
exitTransition = null
reenterTransition = null
returnTransition = null
}
navigationFragmentManager.removeOnBackStackChangedListener(this)
}
})
}
}
Ian Lake
09/18/2020, 2:26 PMJoost Klitsie
09/19/2020, 11:51 AMIan Lake
09/19/2020, 2:54 PMJoost Klitsie
09/19/2020, 6:52 PMJoost Klitsie
09/19/2020, 6:52 PMJoost Klitsie
09/19/2020, 6:53 PMJoost Klitsie
10/05/2020, 2:11 PM<MainNavGraph start=LoggedInGraph>
<LoginGaph>
<WelcomeFragment/>
<UsernameFragment/>
<PasswordFragment/>
</LoginGraph>
<LoggedInGraph>
<HomeFragment/>
</LoggedInGraph>
</MainNavGraph>
I start up the app by deciding, did I log in yes or no, if not I open the LoginGraph on top of whatever destination I am on. That is according as what your article linked.
As you can see, my login flow consists of several screens. Once I finish the login, I pop to the LoginGraph with that destination inclusive and I end up in my logged in graph.
HOWEVER:
The PasswordFragment is the fragment that should be disappearing, but the WelcomeFragment is somehow showing up as well (and NOT the UsernameFragment), resulting in 2 fragments running their onReturnTransition.
It doesn't matter how many fragments I have in the stack that I pop, it is always the first in the stack that does an unwanted appearance. Do you know why this is happening and can it be avoided?
As you can imagine, when I am at the end of my LoginGraph and pop the entire LoginGraph, I of course only want to see the last destination transitioning out.Joost Klitsie
10/05/2020, 2:53 PMIan Lake
10/05/2020, 5:53 PMIan Lake
10/05/2020, 5:55 PMJoost Klitsie
10/05/2020, 5:56 PMIan Lake
10/05/2020, 5:59 PMpostponeEnterTransition(0, TimeUnit.MILLISECONDS)
to just postpone for one frame, no startPostponedEnterTransition()
needed)