Android - Shared element transitions with calling activity finish()
Solution 1
You can finish your activity in the onStop function, if you only want this to happen when you transition from A to B then create a flag and set it after you call startActivity(ctx,intent, bundle):
@Override
public void onStop() {
super.onStop();
if(mShouldFinish)
finish();
}
Make sure when you are done with activity B to call finish() and not finishAfterTranstion() since activity A is no longer there
After finishing the activity A, shared element in B might hang in screen if you press back. Set transitionName to null in ActivityB.onEnterAnimationComplete
to avoid this.
Solution 2
UPDATE
Much better and simpler way
ActivityCompat. finishAfterTransition(this);
<3 support library.
Solution 3
This is maybe late but I had the same issue. What worked for me is:
supportFinishAfterTransition();
This is included in the support library and works like charm.
PS: you don't needto call finish()
when you call supportFinishAfterTransition()
.
Solution 4
Try out finishAfterTransition() method in 5.0 and above you can finish the activity after the exit transition occurs.
Solution 5
If you use ActivityOptions.makeSceneTransitionAnimation(Activity, android.view.View, String)
to make your transition you should use its callback method in Activity B to finish Activity A.
setEnterSharedElementCallback(new SharedElementCallback() {
@Override
public void onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) {
super.onSharedElementEnd(sharedElementNames, sharedElements, sharedElementSnapshots);
// finish Activity A
}
});
F.A.
Updated on July 08, 2022Comments
-
F.A. almost 2 years
I'm working on making an application more Material and I'm just stuck on how to implement some shared element transitions. I have an activity A that starts another B and then calls finish() in order to remove it from the back stack. In my case I have an element I want to share between the two activities, but once it is passed from A to B, A no longer matters. If I don't call finish() after startActivity(ctx,intent, bundle) the exit/enter animation works perfectly. However, if I do call finish, there's a really ugly flicker before the animation starts.
Is there something I'm overlooking or is it just not possible to do what I am trying to do?
-
guness almost 9 yearsI cant seem to find
finishWithTransition
. So I triedfinishAfterTransition
and did not work either. -
ksarmalkar almost 9 yearsIts in support library. Also try bumping your targetSDK version
-
Darren B over 8 yearsThe call is actually: ActivityCompat.finishAfterTransition(this); Just tested and it works great.
-
xsorifc28 over 8 yearsthis still causes flickering for me using a shared element.
-
ksarmalkar over 8 yearswhat do you mean by flickering ? can you upload some gif some where and post link here
-
David Murdoch over 8 years@ksarmalkar,
finishAfterTransition
doesn't callfinish
after the transition is complete; it just callsfinish
immediately. So if you are transitioning from Activity A to B, and B fades in, then A is gone so the Home screen bleeds through (or possibly another Activity). Note: this seems to happen only when using shared transitions. -
Gabor Peto about 7 yearsThis does not work for me, the shared element transition is not correct when I do this. Everything is fine if I don't finish Activity A but I want to do so after the transition is finished. Can't get this to work.
-
extmkv almost 7 years
finishAfterTransition
is just calls after the activity transition, the shared elements is a different transition and you need to do theboolean
trick. -
Antonis Radz over 5 yearsOnly this solution worked when using shared elements transition
-
Demigod over 5 yearsIt almost was great solution for me... but it doesn't work on huawei :)
-
Antonis Radz about 4 years
ActivityCompat. finishAfterTransition(this);
is buggy, sometimes it is finishing, sometimes not, sometimes it is even causing crashes......