https://kotlinlang.org logo
#compose-ios
Title
# compose-ios
v

Vlad

03/27/2024, 2:35 PM
Hey guys. I need App Lifecycle (Minimized/Resumed). I use scenePhase for that:
Copy code
@Environment(\.scenePhase) var scenePhase       
...
            ContentView()
                .ignoresSafeArea(.all)
                .onChange(of: scenePhase) { phase in
                    switch phase {
                    case .active:
                        appLifecycle.start()
                    case .background:
                        appLifecycle.stop()
                    default:
                        break
                    }
The thing it when the scene reports .background - seems like observing a state and/or recompositions already stopped?! So basically when .background happens I want to do LaunchEffect. I wonder if it is how it should be or it is a compose bug? The DisposableEffect's onDispose is not triggered. The composable tree is still there, but seems like
val lifecycleState by viewModel.currentState._collectAsState_()
is stopped.
As I can see easy fix for my case would be to do .stop() in .inactive case and to make sure that when I maximize the app to make sure .distinctUntillChanged() coz when the scenes go from .background to .active the .inactive also will be triggered. And while we are .inactive the state collection still happens. UPD: Easy fix doesn't work when you turn off the screen. The .inactive is not called. Only .background, which I can't deliver to Compose. Shame.
It kinda makes sense right? Why would you draw something on UI when you are in background. The issue is in my case that on my UI there is a webview and I need to notify it that the app is minimized for analytics.
4 Views