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.
I think you should still use the call
startActivityForResult()
directly in fragment, no usegetActivity().startActivityForResult()
.I call the
startActivityForResult()
in Fragment and implement theonActivityResult
in Fragment, theonActivityResult()
is called correctly.you can not call
startActivityForResult()
in activity, otherwise theonActivityResult()
in Fragment will not be called.In my case I did this in my Parent Activity
This will help you
onActivityResult
in FragmentsAnd
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 anListAdapter
in aListView
) in theonViewCreated
. And perform the initialization operations (like show a welcome dialog or something like that) in theonActivityCreated
.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.I hope that something of this helps you.