0

As everyone knows, by default, when going from one activity to another using an intent, the second activity appears to slide in from right to left, and when the back button is pressed, the first activity appears to slide in from left to right. I want to go from one activity to another using an intent so that the second activity appears to slide in from left to right. In other words, I want the transition to look as if the user is pressing the back button.

I checked this but there are only instructions for applying animations to an entire activity, not to one transition from the activity to another using an intent. This is a good example, but I don't know the default duration android uses for its activity transitions (when going to another activity using an intent, and when going back), and I don't want my transition animation to look/feel different from the default android transition animation.

Does anyone know how I can simply apply the back key press animation to an activity transition using an intent? And if not, does anyone know the default duration that android uses for its default activity transitions?

Update: I was able to get a slide animation using getWindow().setExitTransition(new Slide(Gravity.END)), but the transition doesn't look exactly like android's default back key press animation.

Huzaifa
  • 482
  • 1
  • 7
  • 20

2 Answers2

0

I ended up trying to find out the duration by trial and error (bisectional search). I followed this tutorial (which I linked to in the question). I arrived at a value of 233 for the duration.

Step-by-step:

Make an anim folder in the res directory. In that folder make two files:

slide_from_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="-100%p"
        android:toXDelta="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:duration="233" />
</set>

slide_to_right.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0"
        android:toXDelta="100%p"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:duration="233" />
</set>

After the call to start the activity, call overridePendingTransition(), like this:

startActivity(backIntent);
overridePendingTransition(R.anim.slide_from_left, R.anim.slide_to_right);
Huzaifa
  • 482
  • 1
  • 7
  • 20
-1

In your theme, you can use android:windowEnterTransition and android:windowExitTransition

<style name="BaseAppTheme" parent="android:Theme.Material">
  ...

  <!-- specify enter and exit transitions -->
  <item name="android:windowEnterTransition">@transition/explode</item>
  <item name="android:windowExitTransition">@transition/explode</item>
  
  ...
</style>

You can find more information here

or

You can also use overridePendingTransition to override the transition. Please find the example from here

theapache64
  • 10,926
  • 9
  • 65
  • 108
  • Sorry, but the first method sets the animation for the activity. I don't want this to happen every time the activity starts or exits. I only want to set the animation for going from one activity to another. The second method is very good, except that I don't know the default duration for android's slide-in and slide-out animations. – Huzaifa Sep 14 '20 at 12:09