https://kotlinlang.org logo
#compose
Title
# compose
m

Mantas Varnagiris

02/25/2020, 1:14 PM
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

shikasd

02/25/2020, 1:16 PM
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

Mantas Varnagiris

02/25/2020, 1:22 PM
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

Mantas Varnagiris

02/25/2020, 1:41 PM
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

codeslubber

02/25/2020, 3:28 PM
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

Leland Richardson [G]

02/25/2020, 3:45 PM
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
2 Views