Android Fragments 返回片段时,Jetpack合成视图未绘制

qjp7pelc  于 2023-01-31  发布在  Android
关注(0)|答案(1)|浏览(154)

在片段的XML ui代码中使用AbstractComposeView固有的合成视图知道该片段是导航图的一部分(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) {

..... }
kcwpcxri

kcwpcxri1#

解决方案:
您必须在ComposeView上调用.disposeComposition()
在onResume()函数中
示例:

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

相关问题