onActivityResult not call in the Fragment

后端 未结 6 714
野性不改
野性不改 2020-12-03 18:19

The structure of the app is like this:

tabHost (in Activity) -> contains -> TabFragment(extend base container fragment)

1. Th

6条回答
  •  萌比男神i
    2020-12-03 18:33

    For NavHostFragment of Navigation Architecture Component

    If you are using single activity and have fragments inside the NavHostFragment, there is an issue of onActivityResult() of the child fragment of NavHostFragment not getting called.

    To fix this issue, you need to call the onActivityResult() of the child fragments manually from inside the onActivityResult() of the host activity. The host activity is the activity that hosts your NavHostFragment.

    Here's the Kotlin code for onActivityResult() of your host activity:

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        val navHostFragment = supportFragmentManager.findFragmentById(R.id.your_nav_host_fragment)
        val childFragments = navHostFragment?.childFragmentManager?.fragments
        childFragments?.forEach { it.onActivityResult(requestCode, resultCode, data) }
    }
    

提交回复
热议问题