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?
Related
My problem is illustrated by this screen record.
When I click the FAB on FinanceFragment, it will open AddTransactionFragment. But when I navigate to ReminderFragment and go back to FinanceFragment, the AddTransactionFragment is destroyed.
How could I -- similar to the YouTube app when one switches between fragments -- keep AddTransactionFragment visible while I navigate to another fragment?
I tried to add addToBackStack() but this turned out not to be what I was looking for.
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'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
I have two fragments (for ex. fragmentA and fragmentB).
first , in fragmentA use findNavController().navigate(R.id.action_fragmentA_to_fragmentB) to navigate to fragmentB .
then , in fragmentB if you want to back to fragmentA. there are two ways in below :
just press back button : fragmentA's onCreate() won't be called
findNavController().navigate(R.id.action_fragmentB_to_fragmentA) : fragmentA's onCreate() will be called
why?
The reason that the back button doesn't call onCreate of the fragment is by design. Users would not expect the back button to call the onCreate, or create, your fragment again.
As an example, think about when you open the YouTube app on Android and you are shown your home screen, populated with videos based on your interests. When you tap on a video after a bit of scrolling and then midway through the video decide to go back by pressing the back button, you expect the app to go back where you tapped on the video, with the same amount of scrolling you had done, instead of reloading your entire home page again filling with new videos and resetting you to the top of the screen.
Similarly, back button in your app should do the same. If however, you want your back button to behave differently, android does provide a way to do this. Refer to this for that.
I am creating new android application using android architecture components.
Here the scenario is,
I have created one ( no other activities ) main activity where I have put NavHostFragment along with drawerlayout. Now I have fragment for splashscreen, where I made toolbar hidden, and everything works fine.
Now, when after 5 seconds I call other fragment using navigationcontroller, in new fragment(home fragment), instead of showing icon to open drawer, it shows back button there. And on click of back button icon, it loads splashscreen again.
Any suggestions ?
This happens because the navigation graph adds fragments to backstack and when you press the back button that pops backstack and navigates to previously fragment.
I suggest you use an activity as the splash screen for better control your stack and backstack and make it as the launcher and finish it after 5 seconds then start the main activity.
Take a look Principles of Navigation