How to get notified when a transition between activities have finished?
Asked Answered
F

1

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());
Furfural answered 11/8, 2015 at 12:30 Comment(6)
you could look into this androiddesignpatterns.com/2015/03/…Alterant
@k0sh I don't see any events there. Only functions that I should call if I want to postpone the transition...Furfural
because there is no such way.Alterant
@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) ?Furfural
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 github.com/toddway/MaterialTransitions but again, there are no such ways :(Alterant
@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.Furfural
C
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.

Chorus answered 11/8, 2015 at 17:32 Comment(5)
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) ?Furfural
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.Chorus
Nice. Do you know perhaps of a tutorial for this kind of customization?Furfural
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.Furfural
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.Bushmaster

© 2022 - 2024 — McMap. All rights reserved.