onActivityResult For Fragment

2019-02-05 12:12发布

问题:

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.

回答1:

I think you should still use the call startActivityForResult() directly in fragment, no use getActivity().startActivityForResult().

I call the startActivityForResult() in Fragment and implement the onActivityResult in Fragment, the onActivityResult() is called correctly.

you can not call startActivityForResult() in activity, otherwise the onActivityResult() in Fragment will not be called.



回答2:

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


回答3:

The onActivityCreated of the fragment serves another purpose:

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).

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.

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.

  • Another way of do it is fire a BroadCastReceiver in the onActivityResult 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.

I hope that something of this helps you.



回答4:

This will help you onActivityResult in 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());
             }
         }
}