Hey, recently I start using this library in one of my projects and I had to say an amazing library.
As a beginner of this library, I simply followed the wiki and create my ViewModel modules like this.
val mvvmModule = module {
viewModel { MyViewModel(get()) }
}
And inject the MyViewModel in my Activity like this.
class MainActivity : AppCompatActivity() {
val myViewModel : MyViewModel by viewModel()
.......
.......
}
Now one thing that bothers me is that after my activity is destroyed the myViewModel and all inner objects attached with it will be destroyed or not?
TL;DR
Yes, if there were no other references to this ViewModel and its inner objects outside LifecycleOwner, that was passed via ViewModelProviders::of.
This is just Plain Old Java with GC.
If there were no references - it doesn't survive Garbage Collection.
Well, basically, this is being handled by Android and Java.
Koin doesn't handle lifecycle of ViewModels as this job is done by interoperation of LifecycleOwner (Fragment or FragmentActivity), ViewModelStore and ViewModelProvider.
ViewModel is being created by Android, Koin only creates ViewModel.Factory with information how to create specific ViewModel.
More properly it is written in classes LifecycleOwnerExt.kt and ViewModelResolution.kt.
Storing ViewModel is being handled by LifecycleOwner - Fragment or FragmentActivity.
When activity/fragment being destroyed, it clears its ViewModelStore:
// Sources of FragmentActivity of androidx.fragment:fragment:1.0.0
@Override
protected void onDestroy() {
super.onDestroy();
if (mViewModelStore != null && !isChangingConfigurations()) {
mViewModelStore.clear();
}
mFragments.dispatchDestroy();
}
ViewModelStore in its turn calls next code:
// Actual cache and holder of ViewModel
private final HashMap<String, ViewModel> mMap = new HashMap<>();
public final void clear() {
for (ViewModel vm : mMap.values()) {
vm.onCleared();
}
mMap.clear();
}
As a result, if you didn't pass somewhere ViewModel - it would be garbage collected, as this is just plain old Java and you shouldn't pass ViewModel anywhere outside LifecycleOwner with which it is connected.
Most helpful comment
TL;DR
Yes, if there were no other references to this ViewModel and its inner objects outside LifecycleOwner, that was passed via ViewModelProviders::of.
This is just Plain Old Java with GC.
If there were no references - it doesn't survive Garbage Collection.
Well, basically, this is being handled by Android and Java.
Koin doesn't handle lifecycle of ViewModels as this job is done by interoperation of LifecycleOwner (Fragment or FragmentActivity), ViewModelStore and ViewModelProvider.
ViewModel is being created by Android, Koin only creates ViewModel.Factory with information how to create specific ViewModel.
More properly it is written in classes
LifecycleOwnerExt.ktandViewModelResolution.kt.Storing ViewModel is being handled by LifecycleOwner - Fragment or FragmentActivity.
When activity/fragment being destroyed, it clears its ViewModelStore:
ViewModelStore in its turn calls next code:
As a result, if you didn't pass somewhere ViewModel - it would be garbage collected, as this is just plain old Java and you shouldn't pass ViewModel anywhere outside LifecycleOwner with which it is connected.