Onactivityresult() Not Called in New Nested Fragment API

onActivityResult() not called in new nested fragment API

Yes, the onActivityResult() in nested fragment will not be invoked by this way.

The calling sequence of onActivityResult (in Android support library) is

  1. Activity.dispatchActivityResult().
  2. FragmentActivity.onActivityResult().
  3. Fragment.onActivityResult().

In the 3rd step, the fragment is found in the FragmentMananger of parent Activity. So in your example, it is the container fragment that is found to dispatch onActivityResult(), nested fragment could never receive the event.

I think you have to implement your own dispatch in ContainerFragment.onActivityResult(), find the nested fragment and invoke pass the result and data to it.

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 never called in my nested fragment

The event is going to be received in the activity. To have it in Fragment D you have to propagate it.

On your parent activity override onActivityResult and start calling the onActivityResult of your fragments:

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

List<Fragment> fragments = fragmentManager.getFragments();
if(fragments != null){
for(Fragment fragment : fragments){
fragment.onActivityResult(requestCode, resultCode, data);
}
}
}

In your parent fragment you have to do the same thing, but remember to use getChildFragmentManager to get the fragment manager of the fragment

    List <Fragment> fragments = getChildFragmentManager().getFragments();

OnActivityResult not called when nested fragment is using replace instead of add with Don't Keep Activity

Based on the answer from https://issuetracker.google.com/issues/36926548, it solved the problem.

The reason is before the Fragment is created, we should check that savedStateInstace is null first.

In this case in the project of https://github.com/elye/issue_android_onactivityresult_not_called,

the ContainerFragment it should be

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (savedInstanceState == null)
childFragmentManager.beginTransaction().replace(R.id.fragment_container, MainFragment()).commit()
}

And in MainActivity, it should be

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (savedInstanceState == null)
supportFragmentManager.beginTransaction().add(R.id.container, ContainerFragment()).commit()
}

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

Nested Fragment in FragmentTabHost can not onActivityResult

Provided that the UserFragment is your child fragment (a child of ContainerFragment), onActivityResult method does not execute once you return from your activity which has been started with
startActivityForResult.

It was a bug in the support library, I came over similar situation a while ago and it still was not fixed. Honestly, I am not sure whether it is fixed at the moment, you have to find out.

Here is a solution describing how to handle that:

http://inthecheesefactory.com/blog/how-to-fix-nested-fragment-onactivityresult-issue/en

https://gist.github.com/artem-zinnatullin/6916740

Basically the easiest way to make onActivityResult method executed in the child fragment is to start the activity from Activity level getActivity().startActivityForResult() or Parent Fragment getParentFragment().startActivityForResult() and notifying manually every fragment belonging to the calling Activity about invoked Activity's result.

Alternatively you can use some open source:

https://github.com/nuuneoi/StatedFragment

Hope that helps somehow.

Response to the Supplement:

Just as stated above, BaseContainerFragments belong to MainActivity, for those fragments onActivityResult method is called normally.

The problem begins when a child fragment is added to a parent fragment. For the child fragments onActivityResult is not called and you have to handle it manually in the Activity or the ParentFragment.

See :

onActivityResult() not called in new nested fragment API

onActivityResult is not being called in Fragment



Related Topics



Leave a reply



Submit