xiaoyu
xiaoyu

Reputation: 2539

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:

But if I do:

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

Upvotes: 5

Views: 391

Answers (2)

Nemesiss.Lin
Nemesiss.Lin

Reputation: 11

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.

Upvotes: 1

seekingStillness
seekingStillness

Reputation: 5093

Add this to Activity B

@Override
protected void onStop() {
    if(Build.VERSION.SDK_INT == Build.VERSION_CODES.Q && !isFinishing()){
        new Instrumentation().callActivityOnSaveInstanceState(this, new Bundle());
    }
    super.onStop();
}

Upvotes: 0

Related Questions