How to prevent reverse shared element transition
Asked Answered
D

2

9

I have a litle problem.

I have a fragment A with a list and an activity B with also a list. Now A and B have an image in common, so I set up an shared element transition and it works on the way from A to B.

But I don't want to have the reverse transition if I press the back button. So I don't call in B supportFinishAfterTransition instead of finish. But now there is some ugly animation:

B disappears normally except the image. The image stays in foreground until A is visible in the background and then disappears.

How can I prevent this? I want the normal behavior like if I would call finish or onBackPressed.

Greets

Dessalines answered 28/4, 2017 at 14:40 Comment(0)
D
8

I found a solution.

I have played around a bit and if you override onBackPressed() and call finish() instead of super.onBackPressed(), It works.

Dessalines answered 28/4, 2017 at 15:29 Comment(2)
supportLib 27.1.1, i have to use finish() to disable shared elements transition on return and reenter to AConventicle
Hey, see my answer. Much cleaner to override finishAfterTransition()Sarcenet
S
6

just stumbled upon this problem and there's a cleaner solution..

Just override finishAfterTransition(). You should have a boolean in your base class maybe, so it will look like this:

final override fun finishAfterTransition() {
    if (myShouldAllowReverseTransitionsBoolean) {
        super.finishAfterTransition()
    } else {
        super.finish()
    }
}
Sarcenet answered 29/8, 2019 at 9:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.