Hello! , I am starting a new Activity by using `st...
# android
u
Hello! , I am starting a new Activity by using
startActivity(intent)
and
finish()
, but my viewModel
onCleared()
method is not called . What is the reason. I am using koin for di. Appcompat library 'com.android.supportsupport v427.1.1' .
g
Probably your activity leaked
u
But onDestroy is called for the activity.
g
Activity still can be alive. Not sure how ViewModel behaves in this case (activity lifecycle in destroyed state, but activity leaked)
u
How can I find if that's the case. Can you suggest me ways to proceed from here
g
Maybe you could debug your code or check memory dump to check this theory
u
Afaik the ViewModels are (by default, but you can change that) bound to a store owned by a retained Fragment, which lives beyond the lifecycle of the Activity instance (to survive configuration changes). Only when the retained fragment is destroyed will you see `onCleared()`invoked.
Also, it’s not too difficult to trace that from the arch lib source - always recommended.
s
@Udit003 LeakCanary identifies finished activity with active references to it. Can you share a github project reproducing the issue?
u
Sorry guys, Actually it was a silly mistake by me. Rather than injecting the viewmodel using
by viewModel()
, I used
by inject()
. Because of which koin was not able to attach the viewmodel to the lifecycle of activity. But thanks for all your suggestions. In the debugging process, I got introduced to LeakCanary, so it was helpful.
s
@Udit003 I have it enabled it always in Debug builds, helps find out regressions as they occur. But it can't catch all kinds of memory usage issues.
👍 1