33

I'm trying to make an Activity Transition using Shared Elements on a pre-Lollipop device (4.x). Is it possible? So far, I'm trying this:

public class RewardDetail extends ActionBarActivity {
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        ...

        ViewCompat.setTransitionName(imageView, TRANSITION_NAME);
    }

    ...

    public static void launch(ActionBarActivity activity, View transitionView, WelcomeReward detailData) {
        ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, transitionView, TRANSITION_NAME);
        Intent intent = new Intent(activity, RewardDetail.class);
        intent.putExtra(PARAM_DATA, detailData);
        ActivityCompat.startActivity(activity, intent, options.toBundle());
    }
}

called by:

@Override
public void onClick(final View v) {
    int position = recyclerView.getChildPosition(v);
    WelcomeReward welcomeReward = data.get(position);
    RewardDetail.launch(WelcomeRewardActivity.this, v.findViewById(R.id.reward_view), welcomeReward);
}

But it results in a "regular" transition (no shared element). Any ideas?

EDIT

According to this video, it could be done:

https://www.youtube.com/watch?v=RhiPJByIMrM&index=8&list=WL

Is there a library already implementing this for pre Lollipop ?

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Cheborra
  • 2,627
  • 4
  • 23
  • 39
  • 2
    If you are looking for a way to do it in pre-Lollipop, you can use this video as an example: https://www.youtube.com/watch?v=CPxkoe2MraA (note that it requires much more work to implement, however). – Alex Lockwood Nov 18 '14 at 03:10
  • There was a promising update in Support library 24.2.0 - https://developer.android.com/topic/libraries/support-library/revisions.html#24-2-0-api-updates with android.support.transition package but it is backported APIs for View-level only! – Oleksandr Aug 25 '16 at 11:25

4 Answers4

19

No, Activity/Fragment Transitions are not possible on pre-Lollipop devices. According to the documentation:

Start an activity with additional launch information, if able.

In Android 4.1+ additional options were introduced to allow for more control on activity launch animations. Applications can use this method along with ActivityOptionsCompat to use these animations when available. When run on versions of the platform where this feature does not exist the activity will be launched normally.

See also George Mount's answer to this StackOverflow question.

Tom
  • 16,842
  • 17
  • 45
  • 54
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • So... if the activity/fragment transitions are only possible in Lollipop, does this mean that some Google apps use the more complicated approaches (as you provided in the question's comment) to achieve the transition? I am really curious how Google calendar and Keep get it work in lower versions.. – Qianqian Dec 10 '14 at 09:50
  • Google Calendar and Google Keep have probably implemented their own custom libraries for doing this... there is no official API that supports pre-Lollipop devices. – Alex Lockwood Dec 10 '14 at 14:58
  • @Qianqian It certainly isn't impossible to achieve something similar to Fragment transitions in previous versions of the platforms... fragments are essentially just `View` containers, after all. But implementing such a feature might require reworking major parts of your application. On the other hand, it is impossible to implement Activity transitions on previous versions of the platform as they depend on the Render Thread which was added to the framework in Lollipop. – Alex Lockwood Dec 11 '14 at 15:21
  • @AlexLockwood, from your quote, it looks like Activity launch animations should be possible in Android 4.1+ – Price Mar 12 '15 at 13:05
  • 1
    You can try implementing something yourself, but there are no backwards compatible libraries provided by Google. – Alex Lockwood Mar 12 '15 at 13:17
  • @AlexLockwood my app is working fine in Lollipop but it seem to crash in the pre lollipop versions. Is this related or the app should atleast work without transitions in pre lollipop devices. The error log shows android.content.res.Resources$NotFoundException: Resource ID #0x10c000d – Sagar Devanga Apr 22 '15 at 07:48
  • The app will crash if you try to use these APIs before lollipop. – Alex Lockwood Apr 22 '15 at 11:52
  • 3
    That link in your post was replaced for a reason: [Blacklist the use of common link shorteners in posts](//meta.stackoverflow.com/q/313621). – Tom May 30 '19 at 06:01
  • 3
    [This is being discussed on meta](https://meta.stackoverflow.com/q/385606/6282576) – Amir Shabani May 30 '19 at 06:09
8

You can check out this library for activity and fragment transitions for pre lollipop devices

dependencies {
        compile 'com.albinmathew:PreLollipopTransition:1.1.2'
}

https://github.com/albinmathew/PreLollipopTransition

Albin Mathew
  • 411
  • 1
  • 6
  • 13
2

Although the fancy Lollipop Activity/Fragment transitions are not available pre-Lollipop (without the use of a 3rd party library), you can still override the animation used to transition between activities.

Just before/after you start invoke startActivity() you can make a call to [Activity.overridePendingTransition](http://developer.android.com/reference/android/app/Activity.html#overridePendingTransition(int, int)). When you leave your activity, call the same method.

Similarly you can use ActivityOptionsCompat to define a custom animation to use during a transition.

ActivityOptionsCompat opts =
    ActivityOptionsCompat.makeCustomAnimation(getActivity(), R.anim.in, R.anim.out);
startActivity(intent, opts.toBundle());
Dave Jensen
  • 4,574
  • 1
  • 40
  • 45
2

There is a support library, but it does not support (all) transitions on Android versions below 5.0. There are however some alternatives:

Unofficial Compatibility libraries
https://github.com/andkulikov/transitions-everywhere
https://github.com/takahirom/PreLollipopTransition
https://github.com/lgvalle/Material-Animations

Android KitKat
http://www.doubleencore.com/2013/11/new-transitions-framework/ and a sample found in your SDK samples folder.

Posted earlier to a duplicate of this question here: https://stackoverflow.com/a/27344471/1683141

Community
  • 1
  • 1
Mdlc
  • 7,128
  • 12
  • 55
  • 98