Android - Shared element transitions with calling activity finish()

六月ゝ 毕业季﹏ 提交于 2019-11-30 16:56:46
l-l

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.

UPDATE

Much better and simpler way

ActivityCompat. finishAfterTransition(this);

<3 support library.

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() .

Try out finishAfterTransition() method in 5.0 and above you can finish the activity after the exit transition occurs.

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

        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!