简体   繁体   English

片段的onActivityResult

[英]onActivityResult For Fragment

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. 我的基本Activity和片段都没有被触发。 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. 然后它工作,但其他帖子让我认为这是不正确的。

I think you should still use the call startActivityForResult() directly in fragment, no use getActivity().startActivityForResult() . 我认为你应该仍然在片段中直接使用调用startActivityForResult() ,不要使用getActivity().startActivityForResult()

I call the startActivityForResult() in Fragment and implement the onActivityResult in Fragment, the onActivityResult() is called correctly. 我在Fragment中调用startActivityForResult()在Fragment中实现onActivityResult ,正确调用onActivityResult()

you can not call startActivityForResult() in activity, otherwise the onActivityResult() in Fragment will not be called. 你不能在activity中调用startActivityForResult() ,否则不会调用Fragment中的onActivityResult()

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);
    }
}

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. 对于使用setRetainInstance(boolean)保留其实例的片段也很有用,因为此回调告诉片段何时与新活动实例完全关联。 This is called after onCreateView(LayoutInflater, ViewGroup, Bundle) and before onViewStateRestored(Bundle). 这是在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 . 主要是使用片段,您将膨胀并返回onCreateView的视图,在ListAdapter中执行视图操作(如在ListView中设置onViewCreated And perform the initialization operations (like show a welcome dialog or something like that) in the onActivityCreated . 并在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 the onActivityResult of the activity, and if the fragment isn't null , execute a method that handles the come back from the contact list in the fragment. 我会做什么will'be做findFragmentByIdonActivityResult活动的,如果碎片不null ,执行该处理来自片段中的联系人列表后面的方法。

  • Another way of do it is fire a BroadCastReceiver in the onActivityResult of the activity, and register you fragment to listen that broadcast. 另一种方法是在活动的onActivityResult中触发BroadCastReceiver ,并注册片段以侦听该广播。 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. 最后,与第一个一样,如果您没有带有id的片段,则可以在活动中实例化片段,保存引用,并在执行活动的onActivityResult时向其发送消息。

I hope that something of this helps you. 我希望这对你有所帮助。

This will help you onActivityResult in Fragments 这将帮助您在片段中使用onActivityResult

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());
             }
         }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM