ViewModel onCleared() is not called when I close the app


I am using Jetpack Compose with navigation graph and dagger hilt. Let's say I have two screens: home screen and settings screens. I open the app, I go to home > settings. If I close the app (deleting it from recent apps) when I am on the settings screen, it's viewmodel's onCleared() is not getting called. But, if I press the back button and go back to home screen, onCleared() is called. Anyone knows the reason?

This is how I use viewmodel in the settings screen:

@Composable
fun SettingsScreen(
    navController: NavHostController,
) {
    val settingsViewModel = hiltViewModel<SettingsViewModel>()
}

By default, hiltViewModel is tied to the nav graph. The graph should be cleared when I close the app and I expect onCleared() should be triggered.

0
Feb 3 at 12:23 PM
User AvatarKumar
#android#kotlin#android-jetpack-compose#dagger-hilt

No answer found for this question yet.