Onactivityresult Is Not Being Called in Fragment

onActivityResult is not being called in Fragment

The hosting activity overrides onActivityResult(), but it did not make a call to super.onActivityResult() for unhandled result codes. Apparently, even though the fragment is the one making the startActivityForResult() call, the activity gets the first shot at handling the result. This makes sense when you consider the modularity of fragments. Once I implemented super.onActivityResult() for all unhandled results, the fragment got a shot at handling the result.

And also from @siqing answer:

To get the result in your fragment make sure you call startActivityForResult(intent,111); instead of getActivity().startActivityForResult(intent,111); inside your fragment.

android onActivityResult is not being called in Fragment

I think you are calling startActivityForResult in IabHelper class.
You should call Fragment.startActivityForResult not Activity.startActivityForResult.
To do that in class IabHelper you must implement two methods one with activity param and one with fragment param

onActivityResult not call in the Fragment

The reason why this doesn't work is because you are calling startActivityForResult() from within a nested fragment. Android is smart enough to route the result back to an Activity and even a Fragment, but not to a nested Fragment hence why you don't get the callback.
(more information to why that doesn't work here or on stackoverflow)

Now in order to make it work I suggest you manually route the callback to the ChildFragment (=UploadType) in the ParentFragment (=BaseContainerFragment):

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment uploadType = getChildFragmentManager().findFragmentById(R.id.container_framelayout);

if (uploadType != null) {
uploadType.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);

onActivityResult from Activity to Fragment

You are calling Matisse.from() on the Activity which you are getting from the getActivity() method, tit must be returning you your MainActivity and the onActivityResult will be called for the MainActivity where you are doing nothing and calling super.onActivityResult().

Possible Solutions: .

  1. Shift your onActivityResult code logic from your AddMessageFragment to MainActivity
  2. Change you Matisse call from Matisse.from(getActivity()) to Matisse.from(this), because looking at the source code of Matisse it also supports Fragment context.

Using the second solution you should get your onActivityResult callback in the fragment and you won't need to change/shift any other code logic.

Hope this helps :)

onActivityResult not called in fragment android

Override onActivityResult in parent Activity i.e. parent of all fragment

Related Topics

Leave a reply