In my project I have a splash screen, when it is displayed, my app loading some startup data from server, after loading the data shows another screen. For splash screen I create a ViewModel, but it stays in memory all the time. How to destroy it correctly?
Thank you for help!
@HiltViewModel
class SplashViewModel @Inject constructor (private val repository: Repository) {
....
}
@Composable
fun SplashScreen(vm: SplashViewModel) {
...
}
@Composable
fun Navigate() {
val navController = rememberNavController()
NavHost(navController = navController, startDestination = "splash") {
composable("splash") {
SplashScreen(vm = hiltViewModel())
}
composable("main") {
MainScreen(...) // When shows MainScreen, SplashViewModel object still is in memory
}
}
}