首页 > 解决方案 > FragmentManager 正确实例化片段,但内容片段不可见

问题描述

我一直在尝试解决这个问题一段时间。我的应用使用slidingPaneLayout 来显示其中的片段。我的问题是低于 28 的 API,在slidingPaneLayout 上加载片段一段时间后,当实例化新片段时,它们的内容变得部分或完全不可见,但片段本身可以正常工作(即使内容不可见,声音和计时器也可以工作),如果我去背景与应用程序并返回片段的内容重新出现没有任何问题。我不知道究竟是什么导致了这个问题,如果有人以前遇到过这个问题并且有一些迹象,那对我来说会有很大的帮助。

对于slidingPaneLayout,我使用这个库:https ://github.com/umano/AndroidSlidingUpPanel

为了改变我的片段,我使用这个:

fun showFragment(fragment: Fragment, containerId: Int, tag: String) {
    val fragmentTransaction = supportFragmentManager.beginTransaction()
    fragmentTransaction.replace(containerId, fragment, tag)
    fragmentTransaction.addToBackStack(null)
    fragmentTransaction.commit()
}

它应该是什么样子:

在此处输入图像描述

首次出现此问题时会发生什么

图像第一个错误

第一次发出后,所有片段都加载了不可见的内容(橙色箭头来自活动视图)

[ 第一次错误后的图像错误3

PS:我用应用程序上的 canaryleak 纠正了几乎所有的内存泄漏

PS:我使用的是最新版本的 Fragments,androidx.fragment:fragment:1.2.4

编辑:当我在我的片段中强制 onStop 时,如下所示:

var firstTime = true

override fun onResume() {
    super.onResume()
    if(firstTime){
        onStop()
        firstTime = false
   }
}

片段视图正确显示我不知道为什么

标签: androidandroid-fragments

解决方案


推荐阅读