android – `setEnterTransition`仅适用于`ActivityCompat.startActivity`

前端之家收集整理的这篇文章主要介绍了android – `setEnterTransition`仅适用于`ActivityCompat.startActivity`前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在下一个活动中添加一个输入转换.

所以我做了:

getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
window.setEnterTransition(new Slide());

这似乎不起作用.在做了一些试验和错误之后(因为我将这个转换用于其他活动)我发现它在调用后确实有效

ActivityOptionsCompat activityOptionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(activity,view,"some_name");
ActivityCompat.startActivity(activity,new Intent(TourAndLoginActivity.this,LoginActivity.class),activityOptionsCompat.toBundle());

但我没有共享元素(我添加了一个视图来测试它).
无法将“null”添加为共享元素.

这样做真的是强制性的吗?我的解决方法添加一个不可见的共享元素.

解决方法

取自Android开发者文档:

Start an activity using transitions
If you enable transitions and set an exit transition for an activity,the transition is activated when you launch another activity as follows:

startActivity(intent,ActivityOptions.makeSceneTransitionAnimation(this).toBundle());

If you have set an enter transition for the second activity,the transition is also activated when the activity starts. To disable transitions when you start another activity,provide a null options bundle.

https://developer.android.com/training/material/animations.html

因此,首先启用转换,如下所示:

getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
window.setEnterTransition(new Slide());

然后开始活动如下:

startActivity(intent,ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
原文链接:https://www.f2er.com/android/309348.html

猜你在找的Android相关文章