Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you don't have to use the support library then have a look at <a href="https://stackoverflow.com/a/4936159/1560243">Roman's</a> answer.</p> <p>But if you want to use the <strong>support library</strong> you have to use the old animation framework as described below.</p> <p>After consulting <a href="https://stackoverflow.com/a/4819665/1560243">Reto's</a> and <a href="https://stackoverflow.com/a/9856449/1560243">blindstuff's</a> answers I have gotten the following code working.</p> <p>The fragments appear <strong>sliding in from the right</strong> and <strong>sliding out to the left</strong> when back is pressed.</p> <pre><code>FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit); CustomFragment newCustomFragment = CustomFragment.newInstance(); transaction.replace(R.id.fragment_container, newCustomFragment ); transaction.addToBackStack(null); transaction.commit(); </code></pre> <p><strong>The order is important. <em>This means you must call <code>setCustomAnimations()</code> before <code>replace()</code> or the animation will not take effect!</em></strong> </p> <p>Next these files have to be placed inside the <em>res/anim</em> folder.</p> <p><em>enter.xml</em>:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;set&gt; &lt;translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="100%" android:toXDelta="0" android:interpolator="@android:anim/decelerate_interpolator" android:duration="@android:integer/config_mediumAnimTime"/&gt; &lt;/set&gt; </code></pre> <p><em>exit.xml</em>:</p> <pre><code>&lt;set&gt; &lt;translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="-100%" android:interpolator="@android:anim/accelerate_interpolator" android:duration="@android:integer/config_mediumAnimTime"/&gt; &lt;/set&gt; </code></pre> <p><em>pop_enter.xml</em>:</p> <pre><code>&lt;set&gt; &lt;translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="-100%" android:toXDelta="0" android:interpolator="@android:anim/decelerate_interpolator" android:duration="@android:integer/config_mediumAnimTime"/&gt; &lt;/set&gt; </code></pre> <p><em>pop_exit.xml</em>:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;set&gt; &lt;translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="100%" android:interpolator="@android:anim/accelerate_interpolator" android:duration="@android:integer/config_mediumAnimTime"/&gt; &lt;/set&gt; </code></pre> <p>The duration of the animations can be changed to any of the default values like <code>@android:integer/config_shortAnimTime</code> or any other number.</p> <p>Note that if in between fragment replacements a configuration change happens (for example rotation) the back action isn't animated. This is a <a href="https://code.google.com/p/android/issues/detail?id=25994" rel="noreferrer">documented bug</a> that still exists in the rev 20 of the support library. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload