Java 片段的 onActivityResult
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20038880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
onActivityResult For Fragment
提问by Nath5
I currently have a base activity which is hosting a single fragment. Inside the fragment I have a method which starts the contact chooser.
我目前有一个托管单个片段的基本活动。在片段中,我有一个方法可以启动联系人选择器。
private void chooseContacts() {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
When this activity returns how should I capture the results. I have tried adding a
当此活动返回时,我应该如何捕获结果。我试过添加一个
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Handle Code
}
to both my base Activity and the fragment but neither method are being triggered. If possible I would like to have the fragment handle the return so as not to muddy up the activity.
到我的基本活动和片段,但没有触发任何方法。如果可能的话,我希望片段处理返回,以免混淆活动。
Please let me know what the best practice is in this situation.
请让我知道在这种情况下的最佳做法是什么。
Update:
更新:
if I change:
如果我改变:
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
to
到
getActivity().startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
then it works, but other posts have made me think that is incorrect.
然后它起作用了,但其他帖子让我认为这是不正确的。
回答by 4gus71n
The onActivityCreated
of the fragment serves another purpose:
该onActivityCreated
片段的另一个意图:
Called when the fragment's activity has been created and this fragment's view hierarchy instantiated. It can be used to do final initialization once these pieces are in place, such as retrieving views or restoring state. It is also useful for fragments that use setRetainInstance(boolean) to retain their instance, as this callback tells the fragment when it is fully associated with the new activity instance. This is called after onCreateView(LayoutInflater, ViewGroup, Bundle) and before onViewStateRestored(Bundle).
在创建片段的活动并实例化此片段的视图层次结构时调用。一旦这些部分就位,它可用于进行最终初始化,例如检索视图或恢复状态。对于使用 setRetainInstance(boolean) 保留其实例的片段也很有用,因为此回调会告诉片段何时与新活动实例完全关联。这在 onCreateView(LayoutInflater, ViewGroup, Bundle) 之后和 onViewStateRestored(Bundle) 之前调用。
This is extracted from the documentation
这是从文档中提取的
Mainly with a fragment you will inflate and return the view in the onCreateView
, do the view operations (like set an ListAdapter
in a ListView
) in the onViewCreated
. And perform the initialization operations (like show a welcome dialog or something like that) in the onActivityCreated
.
主要以片段,你会膨胀,并在返回的视图onCreateView
中,执行视图操作(如设置ListAdapter
在一ListView
中)onViewCreated
。并在onActivityCreated
.
You have, several choices, I'm not pretty sure about wich is better for your problem:
您有多种选择,我不太确定哪个更适合您的问题:
What I would do will'be do a
findFragmentById
in theonActivityResult
of the activity, and if the fragment isn'tnull
, execute a method that handles the come back from the contact list in the fragment.Another way of do it is fire a
BroadCastReceiver
in theonActivityResult
of the activity, and register you fragment to listen that broadcast. But I think that this is too messy for something so simple.Finally, like the first one, if you don't have a fragment with an id, you can instantiate the fragment in your activity, save a reference, and send it a message when the
onActivityResult
of the activity is executed.
我会做
findFragmentById
的onActivityResult
是在活动中做一个,如果片段不是null
,则执行一个方法来处理从片段中的联系人列表返回。做到这一点的另一种方式是火
BroadCastReceiver
的onActivityResult
活动,并注册你片段收听该广播。但我认为这对于这么简单的事情来说太混乱了。最后,和第一个一样,如果你没有带 id 的片段,你可以在你的活动中实例化这个片段,保存一个引用,并
onActivityResult
在活动的执行时向它发送一条消息。
I hope that something of this helps you.
我希望这对你有帮助。
回答by Zephyr
I think you should still use the call startActivityForResult()
directly in fragment, no use getActivity().startActivityForResult()
.
我觉得你还是startActivityForResult()
直接在fragment里面用call吧,没用getActivity().startActivityForResult()
。
I call the startActivityForResult()
in Fragment and implement the onActivityResult
in Fragment, the onActivityResult()
is called correctly.
我调用了startActivityForResult()
in Fragment 并实现了onActivityResult
in Fragment,onActivityResult()
正确调用了。
you can not call startActivityForResult()
in activity, otherwise the onActivityResult()
in Fragment will not be called.
您不能startActivityForResult()
在活动中调用,否则onActivityResult()
将不会调用 in Fragment。
回答by Kasthuri Shravankumar
This will help you onActivityResult
in Fragments
这将帮助您处理onActivityResult
Fragments
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
startActivityForResult(builder.build(getActivity()), PLACE_PICKER_REQUEST);
And
和
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
Place place = PlacePicker.getPlace(data, getActivity());
}
}
}
回答by SK16
In my case I did this in my Parent Activity
在我的情况下,我在我的父活动中做到了这一点
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}