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
252
votes
4 answers

AndroidViewModel vs ViewModel

With the introduction of the Android Architecture Components library, several new classes were introduced, including AndroidViewModel and ViewModel. However, I'm having trouble figuring out the difference between these two classes. The documentation…
244
votes
31 answers

ViewModelProviders is deprecated in 1.1.0

Looking at the Google docs for ViewModel, they show the below sample code on how to get a ViewModel: val model = ViewModelProviders.of(this).get(MyViewModel::class.java) When using the latest dependency android.arch.lifecycle:extensions:1.1.1 there…
143
votes
13 answers

how to get viewModel by viewModels? (fragment-ktx)

I am working with Single viewModel for the Activity and all of its fragment. So to initialise viewmodel if have to write this setup code in onActivityCreated of all the fragment's override fun onActivityCreated(savedInstanceState: Bundle?) { …
127
votes
8 answers

Observing LiveData from ViewModel

I have a separate class in which I handle data fetching (specifically Firebase) and I usually return LiveData objects from it and update them asynchronously. Now I want to have the returned data stored in a ViewModel, but the problem is that in…
103
votes
12 answers

Manually clearing an Android ViewModel?

Edit: This question is a bit out of date now that Google has given us the ability to scope ViewModel to navigation graphs. The better approach (rather than trying to clear activity-scoped models) would be to create specific navigation graphs for the…
94
votes
6 answers

When is the viewmodel onCleared called

Are ViewModels independent of activity/fragment lifecycles or just their configuration changes. When will they cease to exist and the subsequent onCleared() method called. Can the viewModel be shared with another Activity ? A…
ir2pid
  • 5,604
  • 12
  • 63
  • 107
79
votes
7 answers

LiveData update on object field change

I'm using Android MVVM architecture with LiveData. I have an object like this public class User { private String firstName; private String lastName; public String getFirstName() { return firstName; } public void…
Alireza Ahmadi
  • 5,122
  • 7
  • 40
  • 67
74
votes
4 answers

Use viewLifecycleOwner as the LifecycleOwner

I have a fragment: class MyFragment : BaseFragment() { // my StudentsViewModel instance lateinit var viewModel: StudentsViewModel override fun onCreateView(...){ ... } override fun onViewCreated(view: View,…
user842225
  • 5,445
  • 15
  • 69
  • 119
65
votes
4 answers

DefaultActivityViewModelFactory not found

After migrating the Hilt version from 2.33-beta to 2.35 my project has stopped building with the error given below: A txt version: error: cannot access DefaultActivityViewModelFactory class file for…
Patryk Kubiak
  • 1,679
  • 2
  • 11
  • 17
56
votes
16 answers

LiveData prevent receive the last value when start observing

Is it possible to prevent LiveData receive the last value when start observing? I am considering to use LiveData as events. For example events like show message, a navigation event or a dialog trigger, similar to EventBus. The problem related to…
54
votes
5 answers

Dagger Hilt 'Assisted' and 'ViewModelInject' is deprecated. in Dagger Hilt View Model 1.0.0-alpha03

In Dagger Hilt View Model 1.0.0-alpha01 implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01" implementation 'com.google.dagger:hilt-android:2.28-alpha' kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01' kapt…
Elye
  • 53,639
  • 54
  • 212
  • 474
51
votes
5 answers

MediatorLiveData or switchMap transformation with multiple parameters

I am using Transformations.switchMap in my ViewModel so my LiveData collection, observed in my fragment, reacts on changes of code parameter. This works perfectly : public class MyViewModel extends AndroidViewModel { private final…
49
votes
4 answers

Should I include LifecycleOwner in ViewModel?

LifecycleOwner is currently needed in order for me to create an observer. I have code which creates an Observer in the ViewModel so I attach the LifecycleOwner when retrieving the ViewModel in my Fragment. According to Google's…
Hiroga Katageri
  • 1,345
  • 3
  • 21
  • 40
44
votes
2 answers

Scoping States in Jetpack Compose

In all applications there will always be this three scopes of state: With Compose, a "Per Screen State" could be achieved by: NavHost(navController, startDestination = startRoute) { ... composable(route) { ... val…
40
votes
2 answers

Android MVVM: Activity with multiple Fragments - Where to put shared LiveData?

I have an architectural question about the android ViewModels: Let's say that in my App I have an Activity with two Fragments inside (using a Viewpager). The two fragments do different things (therefore may have their own ViewModel?), but they also…
stamanuel
  • 3,731
  • 1
  • 30
  • 45
1
2 3
99 100