How can I check whether an app is in the backgroun...
# compose-ios
k
How can I check whether an app is in the background or foreground state on iOS? I've been using the following method with Android Compose. How should I do this on iOS?
Copy code
val lifecycleOwner = LocalLifecycleOwner.current
    DisposableEffect(lifecycleOwner) {
        val lifecycle = lifecycleOwner.lifecycle
        val observer = LifecycleEventObserver { _, event ->
            when (event) {
                Lifecycle.Event.ON_PAUSE -> {
                    exoPlayer.playWhenReady = false
                }
                Lifecycle.Event.ON_RESUME -> {
                    exoPlayer.playWhenReady = true
                }
                Lifecycle.Event.ON_DESTROY -> {
                    exoPlayer.run {
                        stop()
                        release()
                    }
                }
                else -> {}
            }
        }
        lifecycle.addObserver(observer)
        onDispose {
            lifecycle.removeObserver(observer)
        }
    }
d
I think this question is overall to Kotlin for iOS. You can ask the same in channels #ios or #kotlin-native
m
In
iOSApp.swift
you can overwrite a few methods like this
Copy code
func applicationDidBecomeActive(_ application: UIApplication) {
    ...
}

func applicationDidEnterBackground(_ application: UIApplication) {
    ...
}
and then call your Kotlin code from there.