使用AbstractComposeView固有的撰写视图在一个片段的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) {
..... }
解决方案:
您必须调用ComposeView
上的.disposeComposition()
onResume()函数
的例子:
override fun onResume() {
super.onResume()
binding.composeHeader.disposeComposition()
}