https://kotlinlang.org logo
#android
Title
# android
u

Udit003

09/14/2018, 6:40 AM
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

gildor

09/14/2018, 6:51 AM
Probably your activity leaked
u

Udit003

09/14/2018, 6:57 AM
But onDestroy is called for the activity.
g

gildor

09/14/2018, 6:58 AM
Activity still can be alive. Not sure how ViewModel behaves in this case (activity lifecycle in destroyed state, but activity leaked)
u

Udit003

09/14/2018, 6:59 AM
How can I find if that's the case. Can you suggest me ways to proceed from here
g

gildor

09/14/2018, 7:02 AM
Maybe you could debug your code or check memory dump to check this theory
u

Ulrik Bech Hald

09/14/2018, 8:29 AM
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

Sam

09/14/2018, 1:47 PM
@Udit003 LeakCanary identifies finished activity with active references to it. Can you share a github project reproducing the issue?
u

Udit003

09/18/2018, 8:24 AM
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

Sam

09/18/2018, 3:07 PM
@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
4 Views