Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared Element Transition is broken between three or more activities

Problem

I want to achieve shared element transition between Activity A and B, and between Activity B and C.

I did everything based on the android transition documentation.

There is no problem between:

  • A => B => back to A

  • A => B and B => C => back to B

But if I do:

  • A => B and B => C => back to B => back to A

The last step will not have any shared element transition (actually not only shared element transition, even there is only fading it could be lost).

I have been looking for solutions everywhere, but it seems everybody only needs A => B (and B => A) shared element transition but doesn't care any more transitions from B => C and back to A.

Example

See an example I created based on android's animation samples, where Activity A = MainActivity, B = DetailActivity, C = DetailDetailActivity. Clicking the button on Activity B will navigate to Activity C.

enter image description here

like image 802
xiaoyu Avatar asked Nov 07 '22 11:11

xiaoyu


1 Answers

Because while executing C => B, it will re-construct a new EnterTransitionCoordinator for B, replacing the old one contains the correct mPendingExitNames which it is important to indicate views need to be transitioned between A and B.

public boolean startExitBackTransition(final Activity activity) {
    ArrayList<String> pendingExitNames = getPendingExitNames(); // here
    if (pendingExitNames == null || mCalledExitCoordinator != null) {
        return false;
    } else {
          // ...
     }
}

The solution seems to be a little bit tricky. You can refer to ThreeActivityTransitionDemo and pay attention to the SharedElementUtils.

like image 136
Nemesiss.Lin Avatar answered Nov 15 '22 07:11

Nemesiss.Lin