Colton Idle
05/13/2025, 3:51 PMsetContent {
MyTheme {
val viewModel = HomeViewModel()
HomeScreen(viewModel)
}
}
Or at the very least the HomeViewModel creation should be wrapped in a remember {}
?Jonathan
05/13/2025, 3:57 PMJonathan
05/13/2025, 3:58 PMremember {}
would matter because on config changes I believe by default the Activity will be recreated so the entire composition will exist the View hiearchy.Jonathan
05/13/2025, 3:58 PMisSystemInDarkTheme()
changes.José González D'Amico
05/13/2025, 4:00 PMval viewModel = HomeViewModel()
line before setContent
Jonathan
05/13/2025, 4:02 PMephemient
05/13/2025, 4:32 PMJonathan
05/13/2025, 4:43 PMTo provide a good user experience, observe the following best practices:
• Be prepared for frequent configuration changes: don't assume that configuration changes are rare or never happen, regardless of API level, form factor, or UI toolkit. When a user causes a configuration change, they expect apps to update and continue to work correctly with the new configuration.
• Preserve state: don't lose the user's state whenrecreation occurs. Preserve the state as described in Save UI states.Activity
• Avoid opting out as a quick fix: don't opt-out ofrecreation as a shortcut to avoid state loss. Opting out of activity recreation requires you to fulfill the promise of handling the change, and you can still lose the state due toActivity
recreation from other configuration changes, process death, or closing the app. It is impossible to entirely disableActivity
recreation. Preserve the state as described in Save UI states.Activity
• Don't avoid configuration changes: don't put restrictions on orientation, aspect ratio, or resizability to avoid configuration changes andCreating new a app in Android Studio does not alter that configuration for the default initial`MainActivity` .recreation. This negatively impacts users who want to use your app in their preferred way.Activity
ephemient
05/13/2025, 4:44 PMJonathan
05/13/2025, 4:48 PMJonathan
05/13/2025, 4:49 PMWinson Chiu
05/13/2025, 5:42 PMColton Idle
05/13/2025, 5:45 PMephemient
05/13/2025, 5:46 PMephemient
05/13/2025, 5:48 PMJonathan
05/13/2025, 5:50 PMColton Idle
05/13/2025, 6:11 PMJonathan
05/13/2025, 6:12 PMremember {}
should be fine as long as your disable activity recreation on config changes.ephemient
05/13/2025, 6:12 PMephemient
05/13/2025, 6:13 PMJonathan
05/13/2025, 6:15 PMrememberSaveable {}
and the view model survive the activity or process recreation.ephemient
05/13/2025, 6:17 PMWinson Chiu
05/13/2025, 6:37 PMrememberRetained
, although you really should just be using the Android VM though, since it sets up a bunch of the owner/lifecycle/state stuff for you.Colton Idle
05/13/2025, 6:42 PMJonathan
05/13/2025, 6:42 PMJonathan
05/13/2025, 6:43 PMJonathan
05/13/2025, 6:43 PMephemient
05/13/2025, 6:45 PMColton Idle
05/13/2025, 7:03 PMPablichjenkov
05/13/2025, 7:44 PMviewModel {ViewModelFactory}
.
Also it is better to disable configuration changes, killing a whole Activity is a big waste of RAM potentially impacting phone energy.
In case of process death, keep in mind the ViewModel will only be recreated with the saved state data aka serializable data. You will lose every state that is not serializable.mattinger
05/14/2025, 3:50 PMval vm by viewModels<MyVM> {
viewModelFactory {
initializer {
MyVM()
}
}
}