返回片段时,Jetpack Compose 视图不绘制

Ous*_*aki 5 android android-fragments android-jetpack-compose

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

..... }
Run Code Online (Sandbox Code Playgroud)

Ous*_*aki 4

解决方案 :

您必须致电.disposeComposition()您的ComposeView

在你的 onResume() 函数中

例子 :

 override fun onResume() {
    super.onResume()
    binding.composeHeader.disposeComposition()
}
Run Code Online (Sandbox Code Playgroud)