I read somewhere on stack overflow-
If you are creating a new app, you can skip using Fragments altogether and just use composable functions to represent your screens.
But while using fragments we can have different viewmodels for different fragments/screens..Can we achieve same with composable functions..like single activity, different composable functions for different screen and different viewmodel for different composable functions?if yes,is this ideal approach?
If you use the Architecture Components ViewModel library, you can access a ViewModel from any composable by calling the viewModel() function.
We recommend screen-level composables use ViewModel instances for providing access to business logic and being the source of truth for their UI state. You should not pass ViewModel instances down to other composables.
A ViewModelStoreOwner is merely an interface. Any class that implements the getViewModelStore() defined by this interface becomes the owner of ViewModelStore . This class then maintains the ViewModelStore and should be responsible to appropriately restoring it when needed.
Jetpack Compose is Android's modern toolkit for building native UI. It simplifies and accelerates UI development on Android. Quickly bring your app to life with less code, powerful tools, and intuitive Kotlin APIs.
But while using fragments we can have different viewmodels for different fragments/screens.
In Compose, you use composable functions to display your screens - no need to use fragments anymore.
Can we achieve same with composable functions..like single activity, different composable functions for different screen and different viewmodel for different composable functions?
You can use different ViewModels for different composable functions if you need them. This is how you can do that:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
DifferentViewModels()
}
}
}
@Composable
fun DifferentViewModels() {
Column {
FirstComposable()
SecondComposable()
}
}
@Composable
fun FirstComposable(firstViewModel: FirstViewModel = viewModel()) {
Text(firstViewModel.value)
}
@Composable
fun SecondComposable(secondViewModel: SecondViewModel = viewModel()) {
Text(secondViewModel.value)
}
class FirstViewModel() : ViewModel() {
val value = "Value from the first View Model"
}
class SecondViewModel() : ViewModel() {
val value = "Value from the second View Model"
}
Make sure to add the ViewModel dependency on your build.gradle(Module) file:
// Compose ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha07"
if yes,is this ideal approach?
It depends on your project requirements. If you have different big screens where you need to store many different values and you don't need to share them with different screens, you can use different ViewModels for them.
If the screens are small and/or you need to share values between them, sharing one ViewModel will be the way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With