Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Activity transitions using Support v4 up to date with Android L

I was implementing material design for my app. I saw that it is possible to make transitions between activities just here: http://android-developers.blogspot.com.es/2014/10/implementing-material-design-in-your.html

So i tried to follow what "Authentic motion" section says:

ActivityMain.java

Intent intent = new Intent();
String transitionName = getString(R.string.transition_album_cover);
…
ActivityOptionsCompat options =
ActivityOptionsCompat.makeSceneTransitionAnimation(activity,
    albumCoverImageView,   // The view which starts the transition
    transitionName    // The transitionName of the view we’re transitioning to
    );
ActivityCompat.startActivity(activity, intent, options.toBundle());

activity_main.xml

<ImageView
    …
    android:transitionName="@string/transition_album_cover" />

activity_details.xml

<ImageView
    …
    android:transitionName="@string/transition_album_cover" />

However, this seems to make the default android activity transition, and I see no animations.

Keep in mind that I called requestWindowFeature(Window.FEATURE_CONTENT_TRANSITIONS) as it says here http://developer.android.com/reference/android/support/v4/app/ActivityOptionsCompat.html#makeSceneTransitionAnimation(android.app.Activity, android.view.View, java.lang.String)

Also all this was tested with a Nexus4 API Level 19

Where is the problem?

like image 582
BamsBamx Avatar asked Nov 01 '14 10:11

BamsBamx


People also ask

What is transition animation in Android?

Android's transition framework allows you to animate all kinds of motion in your UI by simply providing the starting layout and the ending layout.

How do I get my activity back on android?

You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it'll take you back to the previous activity.


2 Answers

ActivityOptionsCompat.makeSceneTransitionAnimation works only on api level >= 21. From docs:

Some material design features like the material theme and custom activity transitions are only available on Android 5.0 (API level 21) and above. However, you can design your apps to make use of these features when running on devices that support material design and still be compatible with devices running previous releases of Android.

Here is it's definition:

public static ActivityOptionsCompat makeSceneTransitionAnimation(Activity activity,
        View sharedElement, String sharedElementName) {
    if (Build.VERSION.SDK_INT >= 21) {
        return new ActivityOptionsCompat.ActivityOptionsImpl21(
                ActivityOptionsCompat21.makeSceneTransitionAnimation(activity,
                        sharedElement, sharedElementName));
    }
    return new ActivityOptionsCompat();
}

So why does that method exist in support package?

It is done so in order to maintain backward compatibility with older versions (api level <=20). From maintaining compatibility docs:

// Check if we're running on Android 5.0 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // Call some material design APIs here
} else {
    // Implement this feature without material design
}
like image 139
Manish Mulimani Avatar answered Nov 15 '22 19:11

Manish Mulimani


Activity Transitions are exclusive to Android 5.0. ActivityOptionsCompat. makeSceneTransitionAnimation doesn't do anything if you're running 19 or below.

like image 23
klmprt Avatar answered Nov 15 '22 18:11

klmprt