What is the difference between enterAnim & popEnterAnim & exitAnim & popExitAnim?
AndroidAndroid AnimationAndroid Architecture-NavigationAndroid Problem Overview
What is the difference between animation tags in latest Navigation Architecture Component? I got confused with enterAnim
& popEnterAnim
. Similarly, exitAnim
& popExitAnim
.
Any visual expansions is more than welcomed.
Android Solutions
Solution 1 - Android
The Animate transitions between destinations documentation details the four types of animations:
> - Entering a destination > - Exiting a destination > - Entering a destination via a pop action > - Exiting a destination via a pop action
"Entering" refers to the destination that is coming onto the screen, while "exiting" refers to the destination leaving the screen.
Therefore when you navigate from destination A
to destination B
, the entering destination B
will have the enterAnim
applied to it and the exiting destination A
will have the exitAnim
applied to it.
When the user hits the system Back button, going from B
back to A
, the reverse happens: the entering destination A
will have the popEnterAnim
applied to it and the exiting destination B
will have the popExitAnim
applied to it.
Solution 2 - Android
Pop is related to the back stack, I use back stack for helping understanding
e.g.
A
is showing on screen now, we are going to put B
on screen, then A
will Exit(Anim/transaction) from screen, B
will Enter to screen.
Now what’s going to happen if you tap back key/up button? Yes, the reverse happens. Which means B
is going to be replaced by A
.
B
is popped off the back stack and Exit from screen (Pop Exit)A
is entered to the view from the back stack (Pop Enter)
Solution 3 - Android
I'm aware that I'm answering a three-year-old question, but I have a slightly different understanding of the enterAnim
, exitAnim
, popEnterAnim
and popExitAnim
than what is presented in the accepted answer by @ianhanniballake.
In my interpretation, all four attributes are targeting the same destination in the graph: the one defined by the app:destination
attribute of the given <action>
. The enterAnim
and exitAnim
is applied when navigating to or from the destination the "regular way", while popEnterAnim
is applied to the destination when it is shown as a result of the destination "above" it being popped from the backstack. Likewise the popExitAnim
is applied to the destination when itself is popped from the backstack.
Let me clarify with an example as well. Let's assume below navigation graph:
<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/example_graph"
app:startDestination="@id/destination_a">
<fragment
android:id="@+id/destination_a"
android:name="com.example.FragmentA">
<action
android:id="@+id/from_a_to_b"
app:destination="@id/destination_b"
app:enterAnim="@anim/slide_in_bottom"
app:exitAnim="@anim/scale_out_center"
app:popEnterAnim="@anim/scale_in_center"
app:popExitAnim="@anim/slide_out_bottom" />
</fragment>
<fragment
android:id="@+id/destination_b"
android:name="com.example.FragmentB">
<action
android:id="@+id/from_b_to_c"
app:destination="@id/destination_c" />
</fragment>
<fragment
android:id="@+id/destination_c"
android:name="com.example.FragmentC" />
</navigation>
As we launch the target Activity we will be looking at FragmentA. In this example we will navigate from FragmentA -> FragmentB -> FragmentC -> Back -> Back, only by using the defined <action>
's and the system back button.
So, navigating from FragmentA to FragmentB will:
- Hide FragmentA with default animations
- Show FragmentB with the
slide_in_bottom
animation ("enterAnim")
Further on, navigating from FragmentB to FragmentC will:
- Show FragmentC with default animations, and
- Remove FragmentB with
scale_out_center
animation ("exitAnim")
On our way back, pressing the system back button will:
- Remove FragmentC from the backstack with default animations
- Show FragmentB with the
scale_in_center
animation ("popEnterAnim")
Pressing the system back button again will:
- Hide FragmentB with the
slide_out_bottom
animation ("popExitAnim") - Show FragmentA with default animations