Hey, is there a way to know when object is no long...
# dagger
p
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
you can use
ActivityRetainedLifecycle
which has
onCleared
callback
p
@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
that’s how i’d do it, sadly there’s no analog, you can maybe open a FR on Github
p
@FunkyMuse and what's about non scoped objects? their lifecycle depends on the caller object lifecycle
f
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