将Bundle传递给startActivityForResult以实现场景过渡 [英] Pass a Bundle to startActivityForResult to achieve Scene Transitions
问题描述
我在玩棒棒糖sceneTransitionAnimations
.
要使其正常工作,您需要在调用活动的onCreate
中实现getWindow().setExitTransition()
+ getWindow().setReenterTransition()
,
以及被叫活动的onCreate
中的getWindow().setEnterTransition()
+ getWindow().setReenterTransition()
.
To get it to work you need to implement getWindow().setExitTransition()
+ getWindow().setReenterTransition()
in the calling activity's onCreate
,
and getWindow().setEnterTransition()
+ getWindow().setReenterTransition()
in the called activity's onCreate
.
然后,当您调用startActivity
时,必须将Bundle
传递给通过调用ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle()
获得的函数.
Then, when you call startActivity
you have to pass a Bundle
to that function that you obtain by calling ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle()
.
这很好.但是,我需要使用startActivityForResult
开始一项活动.此函数仅使用Intent
和requestCode
,而无需使用Bundle
.使用putExtras
将捆绑包放入意图中无效.
This works fine. However I need to start an activity using startActivityForResult
. This function only takes an Intent
and a requestCode
, but no Bundle
. Putting the bundle into the intent using putExtras
did not work.
当我想使用startActivityForResult
时,如何使这些漂亮的棒棒糖过渡效果正常工作?
How do I get these nice Lollipop transitions to work when I want to use startActivityForResult
?
按要求输入代码进行
我在一个片段中,我有一个物品清单.单击某个项目后,我将开始另一个活动.
I am inside a Fragment, I have a list of items. When an item is clicked I start another activity.
Intent intent = new Intent(context, otherActivity.class);
Bunde bundle = null;
if (android.os.Build.VERSION.SDK_INT >= 21)
bundle = ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle();
现在有两个区别.这一项有效:
now here come the two distinctions. This one works:
getActivity().startActivity(intent, bundle);
该片段不提供此功能,因此我必须使用其父级活动-因此是getActivity()
.
The Fragment does not offer this function, so I have to use its parent activity's - hence the getActivity()
.
此操作不起作用:
intent.putExtras(bundle);
startActivity(intent);
推荐答案
感谢Squonk,我意识到我要使用的方法startActivityForResult(Intent intent, int requestCode, Bundle options)
实际上存在.
Thanks to Squonk I have come to realize that the method I am trying to use, startActivityForResult(Intent intent, int requestCode, Bundle options)
actually exists.
我犯了一个错误,即尝试从未实现的Fragment(与startActivity(Intent intent, Bundle bundle)
相同)开始,因此您需要调用getActivity().startActivityForResult(Intent intent, int requestCode, Bundle options)
.
I had done the mistake of trying to start that from a Fragment, where it is not implemented - same as startActivity(Intent intent, Bundle bundle)
- so you need to call getActivity().startActivityForResult(Intent intent, int requestCode, Bundle options)
.
这篇关于将Bundle传递给startActivityForResult以实现场景过渡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!