导航组件:IllegalStateException片段未与片段管理器关联

时间:2019-08-15 15:42:52

标签: android android-fragments android-jetpack android-architecture-navigation android-jetpack-navigation

我正在使用导航组件版本2.1.0-rc01,并使用

在3个屏幕之间来回导航

Navigation.findNavController(it).navigate(R.id.action_participants)

第二次浏览相同的屏幕后,我可以看到第二个片段,但是我收到了异常。我在FragmentManager上启用了日志,似乎有一个未连接的相同片段的差异实例,导致了错误

关于导航组件为何创建未附加片段的另一个实例的任何想法?是否有任何替代方法来获取附加的片段?

    2019-08-15 16:59:30.895 30041-30041/com.app.debug D/FragmentManager:   mName=3-2131361912 mIndex=-1 mCommitted=false
2019-08-15 16:59:30.895 30041-30041/com.app.debug D/FragmentManager:   mEnterAnim=#7f01001e mExitAnim=#7f01001f
2019-08-15 16:59:30.896 30041-30041/com.app.debug D/FragmentManager:   mPopEnterAnim=#7f010020 mPopExitAnim=#7f010021
2019-08-15 16:59:30.896 30041-30041/com.app.debug D/FragmentManager:   Operations:
2019-08-15 16:59:30.896 30041-30041/com.app.debug D/FragmentManager:     Op #0: REPLACE StaffBookingDetailsFragment{82e8301 (97f79b28-d8c1-432a-9e1c-3a781dd42434) id=0x7f0a01c5}
2019-08-15 16:59:30.896 30041-30041/com.app.debug D/FragmentManager:   enterAnim=#7f01001e exitAnim=#7f01001f
2019-08-15 16:59:30.896 30041-30041/com.app.debug D/FragmentManager:   popEnterAnim=#7f010020 popExitAnim=#7f010021
2019-08-15 16:59:30.896 30041-30041/com.app.debug D/FragmentManager:     Op #1: SET_PRIMARY_NAV StaffBookingDetailsFragment{82e8301 (97f79b28-d8c1-432a-9e1c-3a781dd42434) id=0x7f0a01c5}
2019-08-15 16:59:30.897 30041-30041/com.app.debug D/FragmentManager:   enterAnim=#7f01001e exitAnim=#7f01001f
2019-08-15 16:59:30.897 30041-30041/com.app.debug D/FragmentManager:   popEnterAnim=#7f010020 popExitAnim=#7f010021
2019-08-15 16:59:31.935 30041-30041/com.app.debug D/FragmentManager:   mName=4-2131362286 mIndex=-1 mCommitted=false
2019-08-15 16:59:31.935 30041-30041/com.app.debug D/FragmentManager:   Operations:
2019-08-15 16:59:31.936 30041-30041/com.app.debug D/FragmentManager:     Op #0: REPLACE ParticipantsFragment{fdd9ef9 (b7317713-b150-44a2-8b1c-47a0f8c52781) id=0x7f0a01c5}
2019-08-15 16:59:31.936 30041-30041/com.app.debug D/FragmentManager:     Op #1: SET_PRIMARY_NAV ParticipantsFragment{fdd9ef9 (b7317713-b150-44a2-8b1c-47a0f8c52781) id=0x7f0a01c5}
2019-08-15 16:59:55.266 30041-30041/com.app.debug E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.app.debug, PID: 30041
    java.lang.IllegalStateException: Fragment ParticipantsFragment{b6e8bc7 (aa204a1e-5f3a-40c0-86f0-b5edab4b07eb)} not associated with a fragment manager.
        at androidx.fragment.app.Fragment.requireFragmentManager(Fragment.java:910)
        at com.app.bookings.participants.ParticipantsFragment.onParticipantActionClicked(ParticipantsFragment.kt:88)
        at com.app.databinding.ItemBindParticipantBindingImpl._internalCallbackOnClick(ItemBindParticipantBindingImpl.java:218)
        at com.app.generated.callback.OnClickListener.onClick(OnClickListener.java:11)
        at android.view.View.performClick(View.java:6669)
        at android.view.View.performClickInternal(View.java:6638)
        at android.view.View.access$3100(View.java:789)
        at android.view.View$PerformClick.run(View.java:26145)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6863)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

5 个答案:

答案 0 :(得分:5)

确保碎片已被垃圾回收/销毁。如果在onCreateView / onViewCreated等方法中注册了任何不知道生命周期的已注册侦听器(不支持androidx.lifecycle.Lifecycle的侦听器),则不会对片段进行垃圾回收/销毁。确保在片段的onDestroyView()中取消注册此类侦听器。

示例: OnBackPressedDispatcher不了解生命周期。因此,它希望您在该片段被销毁时注销。如果尚未注销,则它会保留一个引用,并在其他片段中按回车键时会被调用。

我在里面调用findNavController()。navigateUp()


        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
             super.onCreate(savedInstanceState)

             val onBackPressedCallback = object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    //some logic that needs to be run before fragment is destroyed
                    findNavController().navigateUp()
                }
            }
            requireActivity().onBackPressedDispatcher.addCallback(
                  onBackPressedCallback
            )
        }

,如果您查看findNavController()的文档

在不是[NavHostFragment]的片段上或在[NavHostFragment]内的片段上调用它会导致[IllegalStateException]

这就是为什么我得到

IllegalStateException片段未与片段管理器关联

解决方案

在onDestroyView中取消注册侦听器

override fun onDestroyView() {
    super.onDestroyView()
    //unregister listener here
    onBackPressedCallback.isEnabled = false
    onBackPressedCallback.remove()
}

答案 1 :(得分:0)

您可以使用导航操作的XML属性popUpToInclusive="true"来指定是否应弹出相同目标的旧实例。 另请参见documentation

答案 2 :(得分:0)

经过进一步的调查,我证实这只是片段未正确处置时的副作用。现在解决了。

答案 3 :(得分:0)

Navigation.findNavController(it).navigate(R.id.action_participants)

代替上面的地方

Navigation.findNavController(context).navigate(R.id.action_participants)

答案 4 :(得分:0)

如果添加生命周期所有者,则无需删除onDestroy Documentation

中的回调
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
             super.onCreate(savedInstanceState)

             val onBackPressedCallback = object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    findNavController().navigateUp()
                }
            }
            // ADD LIFECYCLE OWNER
            requireActivity().onBackPressedDispatcher.addCallback(this,
                  onBackPressedCallback
            )
        }