Startactivityforresult() from a Fragment and Finishing Child Activity, Doesn't Call Onactivityresult() in Fragment

startActivityForResult() from a Fragment and finishing child Activity, doesn't call onActivityResult() in Fragment

You must write onActivityResult() in your FirstActivity.Java as follows

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}

So this will call your fragment's onActivityResult()

Edit: the solution is to replace getActivity().startActivityForResult(i, 1); with startActivityForResult(i, 1);

If I start activity for result from a fragment, should I call on activity result in the fragment or activity?

You must implement the onActityResult for every Fragment which started an activity via startActivityForResult.
So, each fragment can track the result properly: The activity result makes sense only for the fragment which requested it.

There's also a reason for it.

When you start an activity, you have to set a requestCode:

// Note the request code: 0
startActivityForResult(0, new Intent(...))

However, if you call that method from inside a Fragment, Android will internally change the request code (so it can automatically track the fragment which triggered the request).
This happens because the host of a Fragment is a FragmentActivity and not a simple Activity

// From FragmentActivity
// Note how the request Code is overriden ((requestIndex + 1) << 16) + (requestCode & 0xffff)
startActivityFromFragment(...) {
startActivityForResult(this, intent, ((requestIndex + 1) << 16) + (requestCode & 0xffff), options);
}

So, when the onActivityResult is triggered, you can capture the result on the host activity. However, in the host activity, the requestCode is no longer the one that you sent but requestCode changed by Android internally:

In host activity:
@Override
protected void onActivityResult(final int requestCode, final int resultCode,
@Nullable final Intent data) {
// This print 65536
Log.v("TEST", "onActivityResult: " + requestCode);
super.onActivityResult(requestCode, resultCode, data);
}

As you can see, you can capture the activity result on the Host activity. However, the request code is not longer that one you set (0). So, here, you can't track the request code. So, you can't track from who this result is from.

Android will invoke onActivityResult in your fragment. However, before invoking the fragment, the request code is converted back to the value that you sent (0):

// From FragmentActivity
// Note how the request Code is converted back to 0
onActivityResult(....) {
// Android will call your fragment with the correct requestCode
targetFragment.onActivityResult(requestCode & 0xffff, resultCode, data);
}

//In your fragment:
@Override
protected void onActivityResult(final int requestCode, final int resultCode,
@Nullable final Intent data) {
// This print 0. So, now, you know this is the result of the request 0
Log.v("TEST", "onActivityResult: " + requestCode);
}

So, there's also a reason to implement the onActivityResult in your fragment. Specially if you have different fragments starting different activities etc. If you always start same activity, you may wrongly assume you can implement the onActityResult wherever you want. However, that is not true.
Every fragment or activity should handle the activity result they requested. They should not handle the result of other entitities. You can do that but it will only add unnecessary complexity to your code.

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.

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 doesn't call from viewPager fragment

This solution worked for me when I had a ParentFragment that contained a ViewPager with 3 different Fragments (children) and one of the children fragment started an activity.

You can open an activity in a child with getParentFragment().startActivityForResult

Once that activity gets finished it will call onActivityResult in the ParentFragment, where I override the method to call each of the 3 children's onActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
for (Fragment fragment : getChildFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}

onActivityResult on different fragment

You will get onActivityResult() callback inside your activity as both fragments are part of the same activity.

Once you get called inside onActivityResult() check the request code and pop the current fragment(Fragment B).

So now you have only fragment A on your activity, so create another method of onActivityResult() inside your FragmentA and perform your operation.

Refer below link for pop up the fragment.

How to close the current fragment by using Button like the back button?



Related Topics



Leave a reply



Submit