Search code examples
androidandroid-livedataandroid-viewmodelkotlin-stateflow

Should we set LiveData and/or kotlin Flows to null in ViewModel onCleared?


I figured that since both LiveData & Flow should be lifecycle aware that there is no reason to do this. Is there any benefit or detriment to setting them to null yourself in onCleared?


Solution

  • Since both LiveData and Flow are lifecycle-aware they will automatically clean up their resources in the ViewModel's onCleared() method. So there's no need to explicitly set them to null.