Amit Tiwari
Amit Tiwari

Reputation: 3692

Activity Transitions not working

I am trying to implement Activity Transitions but I am not able to see the effects. Here is the code for my first activity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_architecture);
        setUpWindowAnimations();
    }

private void setUpWindowAnimations() {
        if (android.os.Build.VERSION.SDK_INT >= 21) {
            Log.i("ANIM", "Fade called");
            Fade fade = new Fade(2);
            fade.setDuration(3000);
            getWindow().setExitTransition(fade);
        }
    }

Here is the code for second activity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);
        setUpWindowAnimations();
    }

private void setUpWindowAnimations() {
        if (android.os.Build.VERSION.SDK_INT >= 21) {
            Log.i("ANIM", "slide called");
            Slide slide = new Slide(Gravity.LEFT);
            slide.setDuration(3000);
            getWindow().setEnterTransition(slide);
        }
    }

Even though I have set Fade out animation, there is no fading, also, Slide works in default way, i.e. the direction is BOTTOM instead of LEFT.

Here is my values/style.xml and here is my v21/styles.xml.

Here is my AndroidManifest.xml:

<application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/AppTheme">

Why are these transitions not working and how to make them work. I used paste.ubuntu.com because the SO editor was not showing xml properly.

Upvotes: 17

Views: 10092

Answers (2)

Abhishek Tiwari
Abhishek Tiwari

Reputation: 936

Bundle bundle = ActivityOptions.makeSceneTransitionAnimation(this).toBundle();
this.startActivity(intent,bundle);

Add these two lines after you setup your intent between two activities, this will work.

You cannot just start an activity via startActivity(intent) method, you need to specify transitions across activies using bundles.

Upvotes: 44

Zeeshan Shabbir
Zeeshan Shabbir

Reputation: 7114

Declare setUpWindowAnimations(); before setContentView.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setUpWindowAnimations();
        setContentView(R.layout.activity_architecture);

    }

private void setUpWindowAnimations() {
        if (android.os.Build.VERSION.SDK_INT >= 21) {
            Log.i("ANIM", "Fade called");
            Fade fade = new Fade(2);
            fade.setDuration(3000);
            getWindow().setExitTransition(fade);
        }
    }

Other Solution

make a xmlTransition and put this xml code there

<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:interpolator="@android:interpolator/accelerate_decelerate">
    <fade android:fadingMode="fade_out"/>
    <slide android:slideEdge="bottom"/>
</transitionSet>

This should be your Style for Api21

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowContentTransitions">true</item>
        <item name="android:windowTransitionBackgroundFadeDuration">1000</item>
    </style>
</resources>

Then put this code in your activity before setCreateView

if (Build.VERSION.SDK_INT >= 21) {

            TransitionInflater inflater = TransitionInflater.from(this);
            Transition transition = inflater.inflateTransition(R.transition.transition_a);
            getWindow().setExitTransition(transition);
        }

this should be in your other activity before setCreateView

if(Build.VERSION.SDK_INT >= 21){
            Slide slide = new Slide();
            slide.setDuration(1000);
            getWindow().setEnterTransition(slide);
        }

Upvotes: 1

Related Questions