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
17
votes
2 answers

ViewPager with viewmodel and live data , all 6 tabs data is replaced by last tab data

I am working on a ViewPager with 6 tabs where it has only one fragment TimesListFragment Depending on the arguments passed to TimesListFragment it calls api eg; science , technology, travel etc I have followed Google's GithubBrowserSample for my…
17
votes
1 answer

Strange behavior of android's ViewModel

When I try to simulate configuration change in my app by enabling "Don't keep activities" in developer options every time I leave an activity and return, the ViewModel is recreated! Aren't ViewModels supposed to handle these situations? I can…
Mostafa
  • 848
  • 3
  • 10
  • 21
17
votes
2 answers

When to dispose RxJava2 Disposable in ViewModel?

I'm using ViewModel from Android Architecture Components in my app. In the ViewModel, I'm using RxJava2 subscription and I keep Disposable object after I subscribe. Before, when I did this in Activity, I was used to dispose the Disposable in…
Micer
  • 8,731
  • 3
  • 79
  • 73
17
votes
4 answers

LiveData Transformations.map() with multiple arguments

I have a value in the UI that it's value depends on two LiveData objects. Imagine a shop where you need a subtotal = sum of all items price and a total = subtotal + shipment price. Using Transformations we can do the following for the subtotal…
Damia Fuentes
  • 5,308
  • 6
  • 33
  • 65
16
votes
8 answers

Dagger-Hilt: @ViewModelInject is not injecting MyViewModel and crash?

In exploring the ViewModelInject of Dagger-Hilt, I follow the example in https://developer.android.com/training/dependency-injection/hilt-jetpack#viewmodels I try to inject the ViewModel into my activity as follow import…
Elye
  • 53,639
  • 54
  • 212
  • 474
16
votes
4 answers

Should we Use ViewModels to communicate between 2 different fragments or bundles in single activity App Architecture?

Scenario 1 - If we use ViewModels to communicate between fragments, then the ViewModel has to be created by activity reference and hence going to stay there in memory until the activity is destroyed. Scenario 2 - In master-detail flow ViewModel…
16
votes
2 answers

LiveDataScope vs ViewModelScope in Android

I was reading how to use coroutines here https://developer.android.com/topic/libraries/architecture/coroutines. What makes me confused about is the difference between LiveDataScope and ViewModelScope. It sounds like ViewModelScope takes care of…
15
votes
4 answers

Get parent fragment from child when using Navigation Component

I need to transfer data from one fragment to another. Now the recommended way to do this is to use a shared ViewModel. To get the same instance available in both fragments, common owner is needed. As it can be their common Activity. But with this…
Ziens
  • 381
  • 2
  • 8
15
votes
2 answers

The correct way to obtain a ViewModel instance outside of an Activity or a Fragment

I'm building a location app where I display background locations from a Room database in my MainActivity. I can get a ViewModel by calling locationViewModel =…
PrashanD
  • 2,643
  • 4
  • 28
  • 58
15
votes
3 answers

How to read data and listen to changes from Room database in a Service?

i am storing user information in a local room database. In activities and fragments I use AndroidViewModel and LiveData to listen to changes made to the database and update the UI. Now I want to analyze all of the past user data to give…
steasy
  • 203
  • 1
  • 2
  • 8
14
votes
1 answer

When to use withContext?

Currently my code looks like this i have a ViewModel that calls the repository to do some background computations and return a result. ViewModel function is run with viewModelScope.launch(Dispatchers.IO) and then the repository one is a suspend…
14
votes
3 answers

Bindable must be on a member in an Observable class

I'm new in data binding , this is my code but I get this error on building class DatabindingViewModel :ViewModel() { val currentFruitName:LiveData get() = FakeRepository.currentName fun…
Navid Abutorab
  • 1,667
  • 7
  • 31
  • 58
14
votes
3 answers

Why Observers added as observeForever to LiveData must be removed?

I've read on Android LiveData documentation that: You can register an observer without an associated LifecycleOwner object using the observeForever(Observer) method. In this case, the observer is considered to be always active and is therefore…
Ana Paula
  • 8,733
  • 3
  • 16
  • 16
14
votes
7 answers

Cannot create an instance of ViewModel class (Unable to start activity ComponentInfo)

I am using MVVM, Retrofit, LiveData in my project but I get this error before that I saw these links Cannot create an instance of custom ViewModel Cannot create an instance of class ViewModel Error java.lang.RuntimeException: Unable to…
13
votes
2 answers

How to use Viewmodel with Jetpack compose

I am trying to use ViewModel with Jetpack Compose, By doing a number increment. But it's not working. Maybe I'm not using the view model in right way. Heres my Main Activity code class MainActivity : ComponentActivity() { override fun…