0

I used side-effect to emit an event at the onCreate moment, but the Composable is not detecting changes in the flow value.
The "onCreate" logs are not being printed, but the "onStart" event with a 1-second delay is being detected, suggesting that the event is being triggered before the Composable is drawn.
using SharedFlow, it appears that the Composable is unable to catch the already emitted event.

How can I know when the Composable becomes interactable?

here's an example.

@Composable
fun Test() {
    val event = MutableSharedFlow<String>()

    val eventState by event.collectAsState(initial = "initial")
    Log.d("teest", "eventState = $eventState")

    val lifecycleOwner = LocalLifecycleOwner.current
    DisposableEffect(Unit) {
        lifecycleOwner.lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onCreate(owner: LifecycleOwner) {
                lifecycleOwner.lifecycleScope.launch {
                    Log.d("teest", "call onCreate")
                    event.emit("onCreate")

                    delay(1000)
                    event.emit("onStart")
                }
            }
        })
        onDispose { }
    }
}

Log

D/teest: eventState = initial
D/teest: call onCreate
D/teest: eventState = onStart
General Grievance
  • 4,555
  • 31
  • 31
  • 45

0 Answers0