onActivityResult is not being called in Fragment

后端 未结 30 2716
忘了有多久
忘了有多久 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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

提交回复
热议问题