简体   繁体   English

Android - 调用活动完成()的共享元素转换

[英]Android - Shared element transitions with calling activity finish()

I'm working on making an application more Material and I'm just stuck on how to implement some shared element transitions.我正在努力使应用程序更加 Material 化,而我只是坚持如何实现一些共享元素转换。 I have an activity A that starts another B and then calls finish() in order to remove it from the back stack.我有一个活动 A,它启动另一个 B,然后调用 finish() 以将其从后堆栈中删除。 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.就我而言,我想在两个活动之间共享一个元素,但是一旦它从 A 传递到 B,A 就不再重要了。 If I don't call finish() after startActivity(ctx,intent, bundle) the exit/enter animation works perfectly.如果我不在 startActivity(ctx,intent, bundle) 之后调用 finish() 退出/进入动画效果很好。 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?是否有我忽略的事情,或者只是无法做我想做的事情?

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):您可以在 onStop 函数中完成您的活动,如果您只希望在从 A 转换到 B 时发生这种情况,则创建一个标志并在您调用 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确保在完成活动 B 后调用 finish() 而不是 finishAfterTranstion() 因为活动 A 不再存在

After finishing the activity A, shared element in B might hang in screen if you press back.完成活动 A 后,如果按返回,B 中的共享元素可能会挂在屏幕上。 Set transitionName to null in ActivityB.onEnterAnimationComplete to avoid this.ActivityB.onEnterAnimationComplete中将 transitionName 设置为 null 以避免这种情况。

UPDATE更新

Much better and simpler way更好更简单的方法

ActivityCompat. finishAfterTransition(this);

<3 support library. <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() . PS:你不needto调用finish()当你调用supportFinishAfterTransition()

在 5.0 及更高版本中尝试finishAfterTransition()方法,您可以在退出转换发生后完成活动。

I've written a variation of this answer which I find a bit more elegant as you don't need a field.我写了这个答案的一个变体,我觉得它更优雅一些,因为你不需要一个字段。
This is still far from ideal but it works in my use case which is basically a splash screen with a transition to the next screen and I want the splash screen to be closed right away.这仍然远非理想,但它适用于我的用例,它基本上是一个闪屏,可以过渡到下一个屏幕,我希望闪屏立即关闭。 This works because onStop is called when the activity is not visible anymore, thus at that point we can actually close it without causing artifacts (in this case this blinking / flickering)这是有效的,因为当活动不再可见时调用onStop ,因此此时我们实际上可以关闭它而不会造成工件(在这种情况下闪烁/闪烁)

lifecycle.addObserver(object : LifecycleEventObserver {
    override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
        if (event == Lifecycle.Event.ON_STOP) {
            lifecycle.removeObserver(this)
            finish()
        }
    }
})

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.如果您使用ActivityOptions.makeSceneTransitionAnimation(Activity, android.view.View, String)进行转换,则应在 Activity B 中使用其回调方法来完成 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

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM