New to Android development, so forgive me for a silly question.
I see others have asked similar questions but they seem pretty old and I struggle to relate the answers to my code.
I have a one activity with multiple fragments app, and I am using the androidx navigation library.
I notice that my exitAnim is not triggered when I press the back button or back arrow on my fragment.
My navigation xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<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/navigation"
app:startDestination="@id/mainMenuFragment">
<fragment
android:id="@+id/mainMenuFragment"
android:name="com.example.app.MainMenuFragment"
android:label="Main Menu"
tools:layout="@layout/main_menu">
<action
android:id="@+id/action_mainMenuFragment_to_deleteUserFragment"
app:destination="@id/deleteUserFragment"
app:enterAnim="@anim/trans_in" />
</fragment>
<fragment
android:id="@+id/deleteUserFragment"
android:name="com.example.app.Features.DeleteUser.DeleteUserFragment"
android:label="Delete User"
tools:layout="@layout/delete_fragment">
<action
android:id="@+id/action_deleteUserFragment_to_clientSelectDialog"
app:destination="@id/clientSelectDialog" />
<action
android:id="@+id/action_deleteUserFragment_to_mainMenuFragment"
app:destination="@id/mainMenuFragment"
app:exitAnim="@anim/trans_out"
app:popUpTo="@id/mainMenuFragment" />
</fragment>
<dialog
android:id="@+id/clientSelectDialog"
tools:layout="@layout/select_client_dialog_fragment"
android:name="com.example.app.Features.DeleteUser.ClientSelectDialog"
android:label="ClientSelectDialog" >
<action
android:id="@+id/action_clientSelectDialog_to_deleteUserFragment"
app:destination="@id/deleteUserFragment"
app:popUpTo="@id/deleteUserFragment" />
</dialog>
</navigation>
And this is how I set things in my Activity:
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navController = this.findNavController(R.id.myNavHostFragment)
NavigationUI.setupActionBarWithNavController(this, navController)
supportActionBar?.setBackgroundDrawable(ColorDrawable(getColor(R.color.app_green_secondary)))
...
}
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp()
}
}
When I press the back button or the back arrow from deleteUserFragment, the exitAnim for the action_deleteUserFragment_to_mainMenuFragment action is triggered. How do I get the animation to work?
P.S. I thought perhaps I had to add app:popExitAnim="@anim/trans_out" to the action, but that does not work either.
Update: I did some debugging by adding a button which performed the navigation action explicitly (deleteUserFragment->mainMenuFragment), and the animation worked. So the question really is, how to make an animation work with back buttons using navigation?
You don't need to explicitly specify transitions when going back(unless you want to pass some arguments but that doesn't seem to be your case).
From:
<fragment
android:id="@+id/home"
android:name="xx.xxxx.xxxx.ui.main.MainFragment"
android:label="XXXX"
tools:layout="@layout/main_fragment">
<action
android:id="@+id/action_home_to_deliveries"
app:destination="@id/deliveries"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right" />
</fragment>
To:
<fragment
android:id="@+id/deliveries"
android:name="xx.xxxxx.xxxx.ui.delivery.DeliveryFragment"
android:label="@string/deliveries"
tools:layout="@layout/xxxx"/>
Animations for going from deliveries
to home
are already contained in the action_home_to_deliveries
.
All you have to do when you want to leave deliveries
is navigateUp()
.
P.S. Base fragment for the animations actually might help you remove the boilerplate with animation definitions but it won't solve the core of the issue - you will just redefine this in code(not XML).