I'm using Android Studio 3.2 Canary 14 and The Navigation Architecture Component. With this you can define transition animations pretty much as you would when using Intents.
But the animations are set as properties of the actions in the navigation graph, like so:
<fragment
android:id="@+id/startScreenFragment"
android:name="com.example.startScreen.StartScreenFragment"
android:label="fragment_start_screen"
tools:layout="@layout/fragment_start_screen" >
<action
android:id="@+id/action_startScreenFragment_to_findAddressFragment"
app:destination="@id/findAddressFragment"
app:enterAnim="@animator/slide_in_right"
app:exitAnim="@animator/slide_out_left"
app:popEnterAnim="@animator/slide_in_left"
app:popExitAnim="@animator/slide_out_right"/>
</fragment>
This gets tedious to define for all actions in the graph!
Is there a way to define a set of animations as default, on actions?
I've had no luck using styles for this.
It is possible with custom
androidx.navigation.fragment.Navigator
.I will demonstrate how to override
fragment
navigation. Here is our custom navigator. Pay attention tosetAnimations()
methodIn the next step we have to add the navigator to
NavController
. Here is an example how to set it:And nothing special in xml :)
Now each fragment from graph will have alpha transitions
As said, R.anim has the default animations defined:
nav_default_enter_anim
nav_default_exit_anim
nav_default_pop_enter_anim
nav_default_pop_exit_anim
But you can easily override them.
Just create your own four anim resources with the same names in your app module (just to clarify, the id of one of them is
your.package.name.R.anim.nav_default_enter_anim
) and write what animation you'd like.R.anim has the default animations defined (as final):
nav_default_enter_anim
nav_default_exit_anim
nav_default_pop_enter_anim
nav_default_pop_exit_anim
in order to change this behavior, you would have to use custom NavOptions,
because this is where those animation are being assigned to a NavAction.
one can assign these with the NavOptions.Builder:
most likely one would need to create a
DefaultNavFragment
, which extends class androidx.navigation.fragment (the documentation there does not seem completed yet).So you can pass these
NavOptions
to theNavHostFragment
like this:alternatively... when looking at the
attrs.xml
of that package; these animations are style-able:this means, one can define the according styles - and define these, as part of the theme...
one can define them in
styles.xml
: