shuot
shuot

Reputation: 67

How to have different animation transitions for same activity on Android?

Please refer to this image:

https://lh4.googleusercontent.com/-gEW9RFcjpnQ/UTKSd0DSR5I/AAAAAAAAALs/P7i47evr2uY/s503/transitions.png

1- When the application is started, a splash-screen is displayed through an activity

2- Then, execution goes to the Main Activity (and splash-screen activity is finished)

3- User, through button click, can go to screen-1 or screen-2 activities.

3.1- When doing so, the Main Activity is preserved (not finished) and the wanted screen activity is created.

4- Going back from screen-2 or screen-2 activities is done by simply finish them. Then, executions goes back to Main Activity, by default.

What I want is:

1- Default transition animation for (A) on the picture. It means default animation from Splash to Main Activity, from Main Activity to screen-1, and when screen-1 is finished (when returning to Main Activity).

2- A specific transition animation for (B) on the picture. It means when going from Main Activity to screen-2 and when screen-2 is finished (when returning to Main Activity).

As far as I searched here and on the internet, I never found how this could be achieved.

How this can be done?

I succeeded to have different animations from Main Activity->screen-1 and Main Activity->screen-2, but I'm completely unable to have different animation between screen-1->Main Activity and screen-2->Main Activity.

Thank you very much for your help!

Upvotes: 0

Views: 1325

Answers (1)

Emanuel Moecklin
Emanuel Moecklin

Reputation: 28856

You use the following code to start the Activity for screen 2:

startActivityForResult(intent4Screen2, RESULT_ANIMATION);
overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);

In onActivityResult() you put:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case RESULT_ANIMATION:
            overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
            break;
    }
}

RESULT_ANIMATION is a constant, zoom_enter and zoom_exit are your animations like:

<set xmlns:android="http://schemas.android.com/apk/res/android"
      android:interpolator="@android:anim/decelerate_interpolator">
    <alpha
        android:interpolator="@android:anim/accelerate_interpolator"
        android:fromAlpha="0.0" android:toAlpha="1.0"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>

If you need to support Android <2.0 which doesn't support the overridePendingTransition() method, you'll need to use reflection.

Upvotes: 3

Related Questions