【问题标题】:Jetpack Compose view not drawing when coming back to fragment返回片段时 Jetpack Compose 视图不绘制
【发布时间】:2023-01-30 16:00:27
【问题描述】:

使用 AbstractComposeView 固有的 Compose 视图 在片段的 XML ui 代码中 知道这个片段是导航图的一部分(Jetpack 导航) 当我按下后退按钮回到我的片段时,撰写视图只是消失了. 这只是我第一次打开片段时画的。

波纹管查看代码

class ProgressComposeView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AbstractComposeView(context, attrs, defStyleAttr) {

    private var steps = mutableStateOf(0)
    private var currentStep: Int = 0
    private var windowWidth: Int = 0

    @Composable
    override fun Content() {
        ProgressView(steps.value, currentStep, windowWidth)
    }

    fun setData(steps: Int, currentStep: Int, windowWidth: Int) {
        this.steps.value = steps
        this.currentStep = currentStep
        this.windowWidth = windowWidth
    }

}


@Composable
fun ProgressView(totalSteps: Int, currentStep: Int, windowWidth: Int) {

..... }

【问题讨论】:

  • 你能提供片段的代码吗?也许你需要在onViewCreated方法中调用setContent
  • 有同样的问题,setContent{} 代码块放在 onViewCreated 方法中 - 它没有帮助
  • @Rob 我回答了这个问题,请检查我的回答

标签: android android-fragments android-jetpack-compose


【解决方案1】:

解决方案 :

你必须在你的ComposeView上拨打.disposeComposition()

在你的 onResume() 函数中

例子 :

 override fun onResume() {
    super.onResume()
    binding.composeHeader.disposeComposition()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多