Breaker ACT
03/30/2022, 4:17 PMviewModelScope.launch
work like a charm until I’m navigate to other screen => viewModel go to onClear() => Then I’m navigate back and try to call viewModelScope.launch
but it didn’t anything. Any idea for this ?Casey Brooks
03/30/2022, 4:28 PMviewModelScope
gets cancelled once the ViewModel itself is cleared, and you can’t launch new jobs in a cancelled coroutineScope. You might have an issue with how you’re accessing your ViewModel. How are you creating your ViewModel?Breaker ACT
03/30/2022, 4:30 PMprotected open val viewModel: VM by lazy { ViewModelProvider(this)[ViewModelClass::kt] }
Breaker ACT
03/30/2022, 4:32 PMthis
as the viewModelStoreOwner. Any incorrect at hereCasey Brooks
03/30/2022, 4:35 PMby lazy
isn’t what you want, that it will hold onto a ViewModel instance longer than it should. You should probably be using the by viewModels()
function from the fragment-ktx
libraryBreaker ACT
03/30/2022, 4:40 PMBreaker ACT
03/31/2022, 2:49 AMTower Guidev2
04/05/2022, 8:12 AMval viewModel: MyViewModel by activityViewModels<YourViewModel>()
and NOT
val viewModel by viewModels<YourViewModel>()