ViewModel Factory (Class Instances)
In the previous post, ViewModel Factory (Old vs. Newer DSL) , we talked about a more modern ViewModel factory. This practice works well, but there is something dangerous you may miss... Previous Example Take a quick look at this code below and think about where this might go wrong... companion object { private const val MODERN_UI_STATE_KEY = "modern_ui_state" @VisibleForTesting internal fun factory (name: String) = viewModelFactory { initializer { ModernDSLStyleFactoryViewModel( name = name, savedStateHandle = createSavedStateHandle () ) } } @Composable fun get ( name: String = "Modern DSL ViewModel Factory" ): ModernDSLStyleFactoryViewModel { return viewModel ( factory = factory(name)) } } The code above is safe, but that's because we're using literals... ...but if we pass in an object, things get more complicated. New Example We'll s...


.png)