0

i want to make animation transition between fragment and activity, there is no errors but the animation does not work, the intent work without animation.

i tried this code

// Start ChechoutActivity after itemList is populated
                        val intent = Intent(requireActivity(), ChechoutActivity::class.java)
                        intent.putExtra("itemList", itemList)
                        // Apply fade-in and fade-out animations to the intent
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
                        startActivity(intent)
                        // Set custom animations immediately after starting the activity
                        requireActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out)

and my animation xml for fade_in

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromAlpha="0.0" android:toAlpha="1.0"
    android:duration="750" />

fade_out

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromAlpha="1.0" android:toAlpha="0.0"
    android:fillAfter="true"
    android:duration="750" />

2 Answers 2

0

Make sure that your R.anim.fade_in and R.anim.fade_out animation resources are correctly defined and placed in the res/anim directory.

below code for fade in and fade out

fade_in.xml

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@interpolator/decelerate_quad"
    android:fromAlpha="0.0" android:toAlpha="1.0"
    android:duration="500" />

fade_out.xml

<alpha xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@interpolator/accelerate_quad" 
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:duration="400" 
    />

The issue is that you're adding the Intent.FLAG_ACTIVITY_NO_ANIMATION flag to the intent, which explicitly disables the animation. Remove this flag, and the animation should work as expected.

Here's the corrected code:

// Start ChechoutActivity after itemList is populated
val intent = Intent(requireActivity(), ChechoutActivity::class.java)
intent.putExtra("itemList", itemList)
startActivity(intent)
// Set custom animations immediately after starting the activity
requireActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out)

By removing the Intent.FLAG_ACTIVITY_NO_ANIMATION flag, the system will allow the animation to take place.

hope will help you

0
0

Probably the culprit is

intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)

remove it and since you are in a fragment you might also want to do

 requireActivity().startActivity(intent)

Also if you know the Activity from which the Fragment is hosted then instead of using requireActivity() use (Java):

getActivity() 

or its property access syntax (Kotlin):

activity

The result code should be

val intent = Intent(activity, ChechoutActivity::class.java)
                    intent.putExtra("itemList", itemList)
                    activity.startActivity(intent)
                    activity.overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.