Pon*_*pat 5 performance android android-intent android-activity android-transitions
为了让共享元素转换顺利运行,我需要在目标活动中推迟繁重的初始化.见下面的代码:
getWindow().setSharedElementEnterTransition(enterTransition);
enterTransition.addListener(new Transition.TransitionListener() {
@Override
public void onTransitionEnd(Transition transition) {
init();
}
});
Run Code Online (Sandbox Code Playgroud)
但是,如果此活动是从Deep link另一个没有共享元素的活动开始的.过渡永远不会开始,因此onTransitionEnd()永远不会被调用,init()永远不会运行.在这种情况下,我应该init()在活动开始后立即打电话.
我怎么知道过渡将会发生?
编辑
我还想在共享元素转换不可用时运行另一个输入转换.所以在下面回答建议使用postponeEnterTransition()对我的情况不起作用.
看来您最好在接收活动的 onCreate (或其他任何地方)中调用推迟EnterTransition(),在该调用之后实现所有繁重的 init() ,并在初始化完成后通过显式调用 startPostponedEnterTransition() 来释放转换保持。如果没有启动活动所需的共享转换(例如 DeepLink 等),这将直接进入您的重型初始化,没有延迟。
这是代码:
活动 A - 启动共享元素转换
Intent ActivityDemoOneBIntent = new Intent(ActivityDemo1A.this, ActivityDemo1B.class);
String transitionName = getString(R.string.activityTransitionName);
Bundle optionsBundle = getTransitionOptionsBundle(imageViewAnimated, transitionName);
startActivity(ActivityDemoOneBIntent, optionsBundle);
Run Code Online (Sandbox Code Playgroud)
活动 B - “接收”共享元素转换
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo_1_b);
postponeTransition(); // postpone shared element transition until we release it explicitly
// Do all heavy processing here, activity will not enter transition until you explicitly call startPostponedEnterTransition()
// all heavy init() done
startPostponedTransition() // release shared element transition. This can be placed to your listeners as well.
}
private void postponeTransition() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
postponeEnterTransition();
} else {
ActivityCompat.postponeEnterTransition(this);
}
}
private void startPostponedTransition() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
startPostponedEnterTransition();
} else {
ActivityCompat.startPostponedEnterTransition(this);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1503 次 |
| 最近记录: |