onActivityResult is not being called in Fragment

后端 未结 30 2612
忘了有多久
忘了有多久 2020-11-21 04:28

The activity hosting this fragment has its onActivityResult called when the camera activity returns.

My fragment starts an activity for a result with th

相关标签:
30条回答
  • 2020-11-21 05:08

    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.

    0 讨论(0)
  • 2020-11-21 05:08

    Original post.

    FragmentActivity replaces requestCode by a modified one. After that, when onActivityResult() will be invoked, FragmentActivity parses the higher 16 bits and restores the index of the original Fragment. Look at this scheme:

    Enter image description here

    If you have a few fragments at the root level there are no problems. But if you have nested fragments, for example Fragment with a few tabs inside ViewPager, you guaranteed will face with a problem (or already faced it).

    Because only one index is stored inside requestCode. That is index of Fragment inside its FragmentManager. When we are using nested fragments, there are child FragmentManagers, which have their own list of Fragments. So, it's necessary to save the whole chain of indices, starting from root FragmentManager.

    Enter image description here

    How do we resolve this issue? There is common workaround solution in this post.

    GitHub: https://github.com/shamanland/nested-fragment-issue

    0 讨论(0)
  • 2020-11-21 05:08

    I was also facing the same problem once I shifted this block of code outside of a Fragment to a Utility Class, with parentActivity passed as argument,

    Intent intent = new Intent(parentActivity, CameraCaptureActivity.class);
    parentActivity.startActivityForResult(intent,requestCode);
    

    Then I was not getting any value in onActivityResult method of that Fragment, Afterwards, I changed the argument to Fragment, so the revised definition of method looked like,

    Intent intent = new Intent(fragment.getContext(), CameraCaptureActivity.class);
    fragment.startActivityForResult(intent,requestCode);
    

    After that, I was able to get value in onActivityResult on the Fragment

    0 讨论(0)
  • 2020-11-21 05:08

    Kotlin version (In your activity onActivityResult())

     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        //add following lines in your activity
        if(supportFragmentManager?.fragments!=null && supportFragmentManager?.fragments!!.size>0)
         for (i in 0..supportFragmentManager?.fragments!!.size-1) {
             val fragment= supportFragmentManager?.fragments!!.get(i)
             fragment.onActivityResult(requestCode, resultCode, data)
        }
     }
    
    0 讨论(0)
  • 2020-11-21 05:09

    I can add two advices if someone still cannot make it. In Manifest.xml file, make sure the hosting activity didn't finish when call back and the activity to be started has the launch mode as standard. See details as below:

    For Hosting activity, set the no history property as false if have

    android:noHistory="false"
    

    For Activity to be started, set the launch mode as standard if have

    android:launchMode="standard"
    
    0 讨论(0)
  • 2020-11-21 05:10

    I think you called getActivity().startActivityForResult(intent,111);. You should call startActivityForResult(intent,111);.

    0 讨论(0)
提交回复
热议问题