首页 > 解决方案 > Calling startActivity with shared element transition from onActivityResult

问题描述

I'm calling within onActivityResult

startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this, imgv, imgv.transitionName).toBundle())

What I expect to happen
activityB to be started and displayed with a shared element transition

What actually happens
activityB is not started/displayed until minimizing the app and reopening it from recents (animation is also not displayed at this point). Without adding the scene transition it works per-usual and calling this from anywhere else in the code works as expected.

I've tried

I suspect it has something todo with a race condition with the animation framework

标签: androidandroid-animationonactivityresult

解决方案


onActivityResult()onStart()在现代版本的onResume()Android API之间调用。听起来在您的活动恢复之前启动具有共享元素转换的新活动存在问题。

(我不确定为什么会出现这个问题。也许其他用户可以回答。)

要解决此问题,我建议将信息保存在以下onActivityResult()位置,然后在以下位置进行查询onResume()

private boolean launchNextActivity = false;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (...) {
        this.launchNextActivity = true;
    }
}

@Override
protected void onResume() {
    super.onResume();

    if (launchNextActivity) {
        launchNextActivity = false;
        // do the launch
    }
}

推荐阅读