I'm using the SupportFragmentManager to navigate between different fragments (let's call them A, B, C, D for simplicity). The navigation itself works correctly.
The animation works only correctly considering the entering fragments. My code is basically the same for navigating between all fragments:
activity?.supportFragmentManager?.commit {
setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right)
replace(R.id.myNavHostFragment, BFragment())
}
The navigation is only linear, so I navigate from A -> B, B -> C, and C -> D.
The problem is: only the animation of A -> B works correctly.
For all others, during the animation, the wrong 'old' (exit) fragment A is shown in background, while the new one slides in.
So this means:
animation A -> B works fine.
animation B -> C does not work correctly. Fragment C slides in correctly. But while the animation plays, not Fragment B is shown in the background. Instead, somehow Fragment A is shown again.
the same for C -> D. Fragment A is shown, while C should be seen during the animation.
As you can see, I do not add fragments to the backstack. I really can't figure out why Fragment A is always shown during the animation, even if it should be a different one.
In a nutshell: the animation shows always the first fragment (A) as exit fragment, while it should be the previous one instead.
Please check your navigation graph and re-visit all the below attributes shown in below image.
Open your own navigation_graph.xml and then click on Design tab.
You will see below attribute(as shown in image) when you click on navigation direction.
Also, Animation has nothing to do with the back stack of your app.
You need to check Pop Behavior and Launch Options
Also please see if you have set any Flag's incorrectly in your code.
For e.g. FLAG like FLAG_ACTIVITY_CLEAR_TOP
And finally please thoroughly go through this Article
Related
I'm doing a nav graph in an Android project where I have a number of fragments - let's just assume for now that I have 3 (fragment A, B and C).
I have navigation actions between the fragment like this: A -> B, B -> C, and finially a global action always just pointing to A (Since the app is way bigger than 3 fragments and fragment A is a start screen, so it makes sense to have a global action going back to the start screen).
From fragment C I can go back to A with the global action (which is also using popUpTo and Inclusive, to clear the backstack totally).
Now the issue - I'm using transition animations when transitioning between the fragments and they work - except for the global action where I'm using popUpTo. I wish to use a from_left and to_right animation as enter/exit, which works exactly as intended on other fragments, but for the global action the exit animation is wrong (looks kinda like the fragment is moving out to the left of the screen instead of out to the right).
The weird thing is: If I set the popUpTo behaviour to "none", it works perfectly fine, animation plays as intended.
So, is there something one should be aware of about transition animations when using popUpTo in the nav graph?
I am currently trying to use the Android Navigation component to navigate from one fragment (A) to another (B) by animating fragment B up from the bottom of the screen (over fragment A) and subsequently animating fragment B back down when the back button is pressed. I currently have an action defined for this:
<action
android:id="#+id/action_landingFragment_to_bookingFragment"
app:destination="#id/bookingFragment"
app:enterAnim="#anim/booking_screen_in"
app:exitAnim="#anim/nothing"
app:popEnterAnim="#anim/nothing"
app:popExitAnim="#anim/booking_screen_out"
app:popUpTo="#id/landingFragment" />
However, when I navigate to fragment B from fragment A currently, I get a nice smooth slide up from the bottom of the screen, hit the back button and see fragment B disappear instantly to display fragment A again, with no animation. I currently use no custom code for the back button on fragment B (though I tried navigateUp() and popBackStack() in an OnBackPressedCallback to see if I was missing something regarding these. I also wonder if there is an issue with Z-axis of the fragments (similar to this thread) but my normal enter/exit animations work fine and I have tried a more traditional set of animations (slide left/right) in which the enter and exit animations play but the pop animations once again do not.
Can anyone suggest the correct way to do back navigation such that these pop animations should be visible?
I am having an issue creating a back stack with the new android navigation component. I have a main graph with a splash fragment, a loginregister root fragment that has it's own graph, an onboard step 1 fragment with corresponding step 1 graph, step2 fragment with step 2 graph, and a home fragment. When the user logs in, they can go to either step1, step2, or the home. If they go to step2, I want them to be able to hit back and end up at step1, and even better, the last fragment of the step1 graph. I've tried setting global actions and setting popTo, but I often get an error saying that it's ignoring the popTo because step1 isn't part of the back stack. Having a button on the screen to go back so I can call navigate with global action works, but when using the hard back button it will go from step2 to login/register.
Even just a simple case of one graph, fragments A, B, C, D. The user can enter the app at any fragment. If I just try to navigate to fragment C with the BtoC action, then there is an error of this action is unknown to this controller. Currently I have to essentially make a couple navigate calls, one after another.
In on of the navigation training docs it says "And in both cases, you should make the Back button more predictable by inserting into the task's back stack the complete upward navigation path to the app's topmost screen. " But I can't find anywhere that really shows how to do this with the NavController and single activity apps.
Any insight would be appreciated.
I'm trying to make an app which has the DrawerActivity as the main activity. I have implemented 8 fragments within it, which correspond to each item in the drawer. Now, the problem I have is, whenever I try to press the back button to go back to the DrawerActivity from the fragment, I end up exiting the app instead. I've been searching on forums for three days now, and have not found any solution to this. Quite frustrated, I stupidly deleted my code, so I can't really show it right now. Can anyone simply explain to me how I should exit a fragment using the back button, and return to my main activity?
Current scenario:
DrawerActivity contains eight fragments, all independent of each other. Let's call them fragments A through H. When I go to fragment A, I should be able to open the navigationDrawer and head to any other fragments from B to H. However, on pressing the back button, I should also be able to go back to the drawer activity's main page.
Things I have tried which didn't work are
1. Using onBackPressed and popBackStack.
2. Creating custom listener.
3. Using fragmentTransaction.
I can't believe how stupid I am. I was using my Gmail app on my phone when I suddenly realized that the first fragment in the drawer activity is always the default fragment, and the app should always revert to it on the back button press. What I was thinking was that the drawer activity would go back to the original state when back is pressed, but I didn't account for the fact that fragments are part of the activity itself.
Thank you all for trying to understand my problem, which wasn't really a problem at all... It seems I learned something in spite of myself.
Have you tried overriding onBackPressed() without calling the superclass method? (So that you prevent finish() from being called.)
#Override
public void onBackPressed() {
//show Fragment A
}
Call it without the "super.onBackPressed()" and it should work.
You must add your fragment to the android back Stack like this:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.your_container,fragment,fragment.TAG)
.addToBackStack(null)
.commit();
I have a common layout where I have a menu in a DrawerLayout and these menu items replace the visible main fragment in my Activity. The top menu item represents my Home screen(fragment A). When I press something else(fragment B) in the menu I replace A with B and add the transaction to the back stack (A -> B). If I select fragment C from the menu I would like the back stack now to be A -> C. In order to do that I call popBackStack() and then replace A with C.
My issue here is that I would like the transition between B and C to be smooth but when I call popBackStack(), fragment A is visible for a split second before it's replaced with C. So what I want is basically creating a FragmentTransaction where I can pop the back stack without committing it before I replace A with C.
To clarify: I should note that when you are in fragment B you could select something there that would take you deeper so the stack would then be A -> B -> B1. If I press C from the menu at this point I need to clear the backstack and show C WITHOUT flashing A in the process.
Does anyone know if that is possible or does anybody have any other solution to my issue ?
Don't add any of fragment in back stack. Just replace one with another. In this way we will skip to call popBackStack().
Not sure if you've solved this issue, but I have been working on a library that should help you solve your navigation issues!
Since the implementation of the ChildFragmentManager it has given us flexibility in creating back stacks within individual fragments. I am leveraging this in the library. It's accessible in jcenter repository, please see the ReadMe for implementation details!
https://github.com/DMCApps/NavigationFragment
Basically you will need to use a SingleStackNavigationManagerFragment as your base fragment. When the menu item from A->B is clicked, call present(B) on the navigation manager (or the within FragmentA). When you want to add C to the stack but remove B, you can call clearNavigationStackToRoot. This will clear the current navigation stack to the first fragment that you added to the manager (aka FragmentA) without any navigation. Then present(C). This will cause no animation to occur for the clearNavigationStackToRoot, but will show an animation for the present(C).
If you need more clarification I can add a sample to my repository for you. The more samples the more resources people will have to utilize my library!
I have added an example of what you need under the DrawerClearStackExample package in the sample application for the library.
Thanks,
DMCApps