Android Developer
Android Developer

Reputation: 9643

Different viewmodel for different composable functions inside same activity

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?

Upvotes: 9

Views: 4571

Answers (3)

Dino Tw
Dino Tw

Reputation: 3321

I also encounter the same problem. By looking the Google sample code GameViewModel.kt, it seems like you will need to clean the data explicitly.

The view model has the init block to clean data

init {
    resetGame()
}

The same function resetGame() is also called when the game is finished.

Or if you are using navigation component, you could rely on popBackStack, https://stackoverflow.com/a/70141402/691626

Upvotes: 0

Raw Hasan
Raw Hasan

Reputation: 1396

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.

Upvotes: 2

Richard Onslow Roper
Richard Onslow Roper

Reputation: 6835

CAN you do it? Yes. Should you do it? Depends upon your use case, but not a problem at all.

You may choose to do so, but in such cases, the best option is what keeps your code clean (readable and maintainable). So, if you are creating composables for entire screens, I think it is viable to use multiple viewmodels for each since a lot of data would be stuffed into a single one otherwise. However, you should pay attention to the maintenance of the viewmodels. If you initialize the vm inside the composable, there are chances of it being destroyed (especially if you use navigation). You would have to declare it top-level in the activity itself. Multiple viewmodels in a single activity might not be common in the view world, but there is no issue in doing so. So my suggestion would be to go for what is suitable for your specific use case. If the only thing you wanted to ask was whether it can be done and whether there is any issue in doing so, then your answers are - Yes, and no (respectively).

Upvotes: 0

Related Questions