In the MainActivity I create a ViewModel object using:
class MainActivity : ComponentActivity() {
private val viewModel by viewModels<MainViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
if(viewModel.isActive()) {
//Do stuff...
} else {
//Do other stuff...
}
}
}
}
And inside a composable function, I'm injecting it using Hilt:
@Composable
fun ActivateScreen(
viewModel: MainViewModel = hiltViewModel()
) {
Scaffold(
topBar = { /... },
content = {
UiContent(
onClick = {
viewModel.activate()
}
)
}
)
}
But it seems to me that the view model objects are different objects, because when I click activate, the change is not propagated to the MainActivity. I need to restart the app in order to see the change. How to make sure I'm using the same instance? Any help would be greatly appreciated.