Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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());
like image 275
android developer Avatar asked Aug 11 '15 12:08

android developer


1 Answers

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.

like image 61
George Mount Avatar answered Sep 21 '22 04:09

George Mount