compose-multiplatform icon indicating copy to clipboard operation
compose-multiplatform copied to clipboard

Compose for Desktop doesn't emit lifecycle events, only states

Open SebastianAigner opened this issue 9 months ago • 3 comments

1.6.10-rc03.

iOS:

Screenshot 2024-05-19 at 11 28 03

Desktop:

Screenshot 2024-05-19 at 11 28 27

Observed via:

@Composable
fun LifecycleLogger() {
    val lc = LocalLifecycleOwner.current
    LaunchedEffect(lc) {
        launch {
            lc.lifecycle.currentStateFlow.collect {
                println("New State: $it")
            }
        }
        launch {
            lc.lifecycle.eventFlow.collect {
                println("New Event: $it")
            }
        }
    }
}

SebastianAigner avatar May 19 '24 09:05 SebastianAigner

Looking at the impl of eventFlow:

public val Lifecycle.eventFlow: Flow<Lifecycle.Event>
    get() = callbackFlow {
        val observer = LifecycleEventObserver { _, event ->
            trySend(event)
        }.also { addObserver(it) }

        awaitClose { removeObserver(observer) }
    }.flowOn(Dispatchers.Main.immediate)

Apparently it uses Dispatchers.Main, which is not available if you don't add coroutines-swing to the project explicitly. Adding this dependency fixes the issue:

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-swing:1.8.1")

zsmb13 avatar May 19 '24 10:05 zsmb13

Very strange that I'm seeing no indication (warning, error, log output) indicating this.

SebastianAigner avatar May 19 '24 10:05 SebastianAigner

It's expected because kotlinx-coroutines-swing is optional. I'll check why a warning message is missed + make sure that it's desired in docs

MatkovIvan avatar May 21 '24 13:05 MatkovIvan