I have a question. I'm not sure if this is for som...
# compose
m
I have a question. I'm not sure if this is for some reason intended or a bug. In Activity if I do this
Copy code
setContent {
    onActive {
        Log.d("ASD", "onActive")
    }
    onDispose {
        Log.d("ASD", "onDispose")
    }
}
1. Start app -
onActive
is called 2. Press back to leave app -
onDispose
NOT called 3. Open app again -
onActive
is called So if we depend on
onDispose
to cleanup resources and it's not called - leaks happen
s
Noticed the same issue with
@Model
class. When subscribing to a static model,
onDispose
is not called and
onActive
is called twice after restarting activity (with don't keep activities).
m
Yes so if you use those leaked resources they get duplicated every time you restart the app
Doe sit work if you do
setContent {}
in
onDestroy
?
m
Ah yes this this seems related. Good catch.
OK so seems like the workaround for now is to store a reference to
Composition
returned by
setContent {}
and then call
Composition.dispose()
inside
onDestroy
in Activity
👍 3
Thanks @mbonnin for pointing this out!
👍 1
c
Was just thinking yesterday that like politics, programming tends to solve some problems, and others just keep reappearing. The C++ community lost their mind over destructors, and of course when Java came along, it was impossible to do anything on destruction because it might not happen until the memory was collected…
l
to clarify though, onDispose is not a GC-triggered lifecycle in any way. it’s a compose-specific lifecycle. The code snippet above not working as expected is a bug, not a misunderstanding. Sorry about that ;)
👍 2