14

Just as the title says...

I'm using a transition between activities, and I'd like to have some kind of listener (or event) for both activities, for when the transition has finished and for just before it started.

Here's a sample code of creating the transition:

    final Intent intent = new Intent(activity, TargetActivity.class);
    if (initialQuery != null)
        intent.putExtra(EXTRA_INITIAL_QUERY, initialQuery);
    final String transitionName = activity.getString(R.string.transition_name);
    ViewCompat.setTransitionName(viewToTransitionFromAndTo, transitionName);
    final ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity,
            viewToTransitionFromAndTo, transitionName);
    ActivityCompat.startActivityForResult(activity, intent, requestCode, options.toBundle());
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • you could look into this http://www.androiddesignpatterns.com/2015/03/activity-postponed-shared-element-transitions-part3b.html – Kosh Aug 11 '15 at 12:33
  • @k0sh I don't see any events there. Only functions that I should call if I want to postpone the transition... – android developer Aug 11 '15 at 12:38
  • because there is no such way. – Kosh Aug 11 '15 at 12:48
  • @k0sh There isn't? What if I want to show a view or animate a view right after the transition ends, then? Can I at least customize the transition to treat some views on the new activity differently (fade in when transition ends) ? – android developer Aug 11 '15 at 13:32
  • There are no such ways in the framework as far as i know. You can have a look.at this simple, you could explore more about transition https://github.com/toddway/MaterialTransitions but again, there are no such ways :( – Kosh Aug 11 '15 at 13:44
  • @k0sh OK, so what's the duration of the transition animation? Maybe I could use a handler to delay something based on it? It's just that the Explode effect makes an item go above something else, and I'd like to change it. – android developer Aug 11 '15 at 14:08

1 Answers1

28

You can add a listener to any of the transitions that you use. For example:

getWindow().getSharedElementTransition().addListener(listener);

This will listen for when the transition itself starts and ends. However, it doesn't give you the whole activity transition information. For example, the calling activity doesn't know when the called activity has finished its transition.

Assuming the transition on top is not marked translucent, the underlying transition will be told to stop -- onStop() -- when the top activity has become opaque. That doesn't mean the transition has finished, it just means that the fade in of the top activity has finished. I can't think of much that you'd want to do once the activity has stopped, though. This won't help when the activity is translucent, though.

So, no, if you want to have both activities know about the transition, you'll have to hack it in. The called activity always knows when the transition finishes (using the listener) on enter and the calling activity always knows on exit.

George Mount
  • 20,708
  • 2
  • 73
  • 61
  • That's enough for what I need. Will check it out when I can. Thank you. Have a +1 for now. Can you please answer this one too: is it possible to make the transition (more specifically the explode transition) ignore specific views, or let me choose what to do with them (maybe a different transition for them) ? – android developer Aug 11 '15 at 18:16
  • 1
    Yes, you can target or exclude views by name, instance, ID, or class. Use an animator set that includes both the explode that excludes the views you don't want and the other transition (fade, for example) that targets only those views. – George Mount Aug 12 '15 at 00:39
  • Nice. Do you know perhaps of a tutorial for this kind of customization? – android developer Aug 12 '15 at 05:11
  • 2
    Anyway, the solution seems to work for me, though you've got the name of the function wrong. It's supposed to be getSharedElementEnterTransition() or any of the others. – android developer Aug 12 '15 at 12:10
  • 2
    Does anyone know how to add a listener for `ActivityOptionsCompat.makeCustomAnimation` instead? I'm not sure which transition I should add it to, or where it should go. I want to listen for the end of the reenter/exit transition in the resuming activity. – androidguy Dec 23 '16 at 08:09