https://kotlinlang.org logo
#dagger
Title
# dagger
p

Philip Dukhov

02/06/2023, 2:53 PM
Hey, is there a way to know when object is no longer available so subscriptions, coroutine scope, etc, can be cancelled? Like with
ActivityScoped
, when activity is destroyed - in this particular case I could've get an activity with
ActivityContext
and listen to the lifecycle, but this won't work generally, e.g. with
ActivityRetainedScoped
. The only idea that come to my mind is using
finalize
but it seems to be a bad practice and doesn't seems to be reliable - as I understand there's no guarantee it'll be called on time. Usually
Closeable
is used in such situations, I've tried implementing it but Dagger doesn't seems to be checking it.
f

FunkyMuse

02/07/2023, 9:04 AM
you can use
ActivityRetainedLifecycle
which has
onCleared
callback
p

Philip Dukhov

02/07/2023, 2:07 PM
@FunkyMuse cool, that's what I was looking for! and there's no analog for
ActivityContext
- I need to cast context to activity and listen the lifecycle, right?
f

FunkyMuse

02/07/2023, 2:09 PM
that’s how i’d do it, sadly there’s no analog, you can maybe open a FR on Github
p

Philip Dukhov

02/07/2023, 4:07 PM
@FunkyMuse and what's about non scoped objects? their lifecycle depends on the caller object lifecycle
f

FunkyMuse

02/07/2023, 8:13 PM
You don't have to scope an object if it's parent is scoped, that instance (child in this case) will be scoped as well (inherit) it's parent's lifecycle, the child may have dependencies too and they'll inherit from it etc... So yes it depends on the caller (unless a listener with initialize/clear which you'd know to handle yourself)
Scoping is expensive, avoid it as much as possible
7 Views