Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose view not drawing when coming back to fragment

Using a Compose view that inherent from AbstractComposeView inside an XML ui code of a fragment Knowing that this fragment is part of a navigation graph (Jetpack navigation) When i press the back button going back to my fragment, the compose view just disappeared. It's only drawing for the first time i open the fragment.

Bellow view code

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) {

..... }
like image 896
Oussaki Avatar asked Jan 27 '26 21:01

Oussaki


1 Answers

Solution :

You have to call .disposeComposition() on your ComposeView

In your onResume() function

Example :

 override fun onResume() {
    super.onResume()
    binding.composeHeader.disposeComposition()
}
like image 113
Oussaki Avatar answered Jan 30 '26 11:01

Oussaki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!