Dontsu
03/31/2026, 5:42 AM@Stable was being used on a ViewModel. My understanding is that since ViewModels are inherently "unstable," the developer used @Stable to prevent unnecessary recompositions.
@Stable
data class MyScreenState(val age: Int)
@Stable
@HiltViewModel
class MyViewModel @Inject constructor(): ViewModel() {
private val _uiState = MutableStateFlow(MyScreenState())
val uiState: StateFlow<MyScreenState> = _uiState.asStateFlow()
}
@Composable
fun MyScreen(
viewModel: MyViewModel() = viewModel()
) {
val uiState by viewModel.uiState.colletAsStateWithLifecycle()
}
However, I’m questioning whether it’s actually necessary to use @Stable on a ViewModel. While I understand the intent, I’m not convinced it’s essential.
For instance, if I structure the code as shown below,
@Composable
fun MyScreenRoot(
viewModel: MyViewModel() = viewModel()
) {
val uiState by viewModel.uiState.colletAsStateWithLifecycle()
MyScreen(uiState)
}
@Composable
fun MyScreen(
uiState: MyUiState
) {
// do something here
}
I can still use Previews, and I don't anticipate significant recompositions occurring simply because the ViewModel itself is considered unstable.
I’m curious to hear your thoughts on this.Ernestas
03/31/2026, 5:57 AMDontsu
03/31/2026, 6:01 AMAlbert Chang
03/31/2026, 6:31 AMDontsu
03/31/2026, 7:40 AMPablichjenkov
04/01/2026, 1:53 AM