Questions tagged [android-viewmodel]

The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations. For topics related to Android, use Android-specific tags such as android-intent, android-activity, android-adapter, etc. For questions other than development or programming, but related to the Android framework, use this link: https://android.stackexchange.com.

UI controllers such as activities and fragments are primarily intended to display UI data, react to user actions, or handle operating system communication, such as permission requests. Requiring UI controllers to also be responsible for loading data from a database or network adds bloat to the class. Assigning excessive responsibility to UI controllers can result in a single class that tries to handle all of an app's work by itself, instead of delegating work to other classes. Assigning excessive responsibility to the UI controllers in this way also makes testing a lot harder.

It's easier and more efficient to separate out view data ownership from UI controller logic and ViewModel helps in separating the data and the UI for the data. Architecture Components provides ViewModel helper class for the UI controller that is responsible for preparing data for the UI. ViewModel objects are automatically retained during configuration changes so that data they hold is immediately available to the next activity or fragment instance.

1739 questions
36
votes
1 answer

What is ViewModelStore and viewModelStoreOwner?

I am very confused due to this new ViewModelProvider api(ViewModelProviders is deprecated) As with the new changes there are new Constructors also (Source code). #1 public ViewModelProvider(@NonNull ViewModelStoreOwner owner) { …
Anmol
  • 8,110
  • 9
  • 38
  • 63
34
votes
8 answers

Unit test the new Kotlin coroutine StateFlow

Recently, the class StateFlow was introduced as part of Kotlin coroutines. I'm currently trying it and encountered an issue while trying to unit test my ViewModel. What I want to achieve: testing that my StateFlow is receiving all the state values…
agonist_
  • 4,890
  • 6
  • 32
  • 55
33
votes
5 answers

Sharing viewModel within Jetpack Compose Navigation

Can anyone suggest how to share a ViewModel within different sections of a Jetpack Compose Navigation? According to the documentation, viewModels should normally be shared within different compose functions using the activity scope, but not if…
33
votes
2 answers

Retrofit call in Kotlin Coroutines viewModelScope

Recently I've updated my ViewModel to use new viewModelScope. From its implementation, I see that Dispatchers.Main.immediate is set as the default CoroutineDispatcher for viewModelScope. So when printing the current Thread in viewModelScope.launch…
Tiko
  • 851
  • 8
  • 15
31
votes
7 answers

Jetpack Compose pass parameter to viewModel

How can we pass parameter to viewModel in Jetpack Compose? This is my composable @Composable fun UsersList() { val myViewModel: MyViewModel = viewModel("db2name") // pass param like this } This is viewModel class…
30
votes
4 answers

Scoping a viewmodel to multiple fragments (not activity) using the navigation component

I'm using the navigation component, I want a view model to be shared between a few fragments but they should be cleared when I leave the fragments (hence not scoping them to the activity) I'm trying to take the one activity many fragments approach.…
30
votes
1 answer

Is it correct to bind a ViewModel to a Service?

I've started using Architecture Components in my application and I'm still learning how to use it. In my app I have an Activity showing different Fragments sequentially. In some of them I need to communicate with a background service in order to…
26
votes
4 answers

How to pass runtime parameters to a ViewModel's constructor when using Hilt for dependency injection?

I'm wondering how to pass runtime parameters to a ViewModel's constructor while using Hilt for DI? Prior to using Hilt, I have a ViewModel that looks like this: class ItemViewModel(private val itemId: Long) : ViewModel() { private val repo =…
26
votes
5 answers

Android ViewModel and startActivity

I am learning ViewModel and LiveData and, in the process, a doubt arose. What should I do if I need to start an Activity? Is it ok to pass the context as a parameter to the ViewModel (the context will not be stored inside the…
26
votes
2 answers

Setting up LiveData observer in custom view without LifecycleOwner

I'm trying out the new Android Architecture components and have run into a road block when trying to use the MVVM model for a custom view. Essentially I have created a custom view to encapsulate a common UI and it's respective logic to use…
25
votes
2 answers

How to use a ViewModelProvider.Factory when extends from AndroidViewModel

I want to send an extra parameter to my ViewModel, but this extends from AndroidViewModel. How can I add this parameter to the ViewModelFactory class ? ViewModel class ProjectViewModel(application: Application) : AndroidViewModel(application) { …
24
votes
4 answers

View models for RecyclerView items

My activity has a Google's ViewModel that fetches some model items. These items are then transformed into adapter items of a RecyclerView. There are also many types of adapter items supported by one RecyclerView. I would like to have separate view…
24
votes
3 answers

Navigation with MVVM android

I have an app using Androids ViewModel class and Navigation Component for navigating between fragments. How would I handle navigation from the ViewModel? I am using RxJava and I was thinking of having the Fragments listen for navigation events and…
23
votes
4 answers

Sharing Data between ViewModels

I have a complex screen in my project which I'm breaking in more than one fragment. I'm trying to follow the MVVM architecture for these classes, so which fragment has its own ViewModel and Contract class. The issue is that all the ViewModels needs…
Igor Escodro
  • 1,307
  • 2
  • 16
  • 30
23
votes
6 answers

Failed to resolve: androidx.lifecycle:lifecycle-extensions-ktx:2.0.0-alpha1

I am trying to understand ViewModel and LiveData concepts in android. I am making a practice project but when i added implementation 'androidx.lifecycle:lifecycle-extensions-ktx:2.0.0-alpha1' line in my app level gradle file it shows me Failed to…