onActivityResult For Fragment

 ̄綄美尐妖づ 提交于 2019-11-30 07:53:18
Zephyr

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.

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

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.

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());
             }
         }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!