Android activity back transition brakes when going back 2 screens

Viewed 256

I made a simple sample to prove that there is something wrong with ActivityOptions.makeSceneTransitionAnimation(activity).

I got 3 activities: A, B & C. Flow is simple: A -> B -> C

All activities share the same style:

<style name="TransitionsTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="android:windowEnterTransition">@transition/slide_in_from_bottom</item>
    <item name="android:windowExitTransition">@transition/slide_out_to_bottom</item>
</style>

Whenever I go: A -> B -> C -> B (back button) -> A (back button) last animation(A -> C) is not played.

It works fine when only doing A -> B -> A (back button)

Here is how I start activities:

fun start(activity: Activity) {
            val intent = Intent(activity, TransitionActivityA::class.java) //or B or C
            val transitionsOptions = ActivityOptions.makeSceneTransitionAnimation(activity)
            activity.startActivity(intent, transitionsOptions.toBundle())
        }

App demonstrating the issue: https://github.com/jkwiecien/AndroidCaseStudies/tree/transitions Use branch transitions

Is that an Android bug or am I doing something wrong?

1 Answers

It does not work, because ActivityB is "started" by ActivityC, while it did work for the transition to ActivityB, because ActivityC is started by it.

It seems that you have to override onBackPressed() and start ActivityA from ActivityB manually, using reversed ActivityOptions. The only way of doing that is calling overridePendingTransition() after finish().

Related