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
22
votes
1 answer

How to mock the view model with Hilt for unit testing fragments?

I've got an android app setup for dependency injection using Hilt, and would like to unit test my fragments. I'm currently creating my view model using: private val viewModel: ExampleViewModel by viewModels() And I am creating the fragment for…
kmell
  • 223
  • 2
  • 4
21
votes
2 answers

How to use Hilt to inject a safe-args argument into a viewmodel?

I have found a similar question here. At the time of writing this question there is only this answer avaliable, which does not provide any help to me, and I believe also to the person who asked the question. I checked the repo which is linked in the…
21
votes
2 answers

Does ViewModel survive Activity save and restore?

Instances of the new ViewModel class can survive configuration changes if used in the following fashion: mViewModel = ViewModelProviders.of(this).get(MyViewModel.class); However, in addition to configuration changes, there is also a save-restore…
20
votes
2 answers

How ViewModel survives configuration change

I am trying to use ViewModel in my app. The question comes to my mind is How View Model survives configuration changes. I read number of blog posts saying that " It will create a HolderFragment to add to your activity or your fragment, it's…
20
votes
3 answers

Updating UI using ViewModel and DataBinding

I am trying to learn ViewModel in android, in my first phase of learning I am trying to update UI (TextView) by using ViewModel and DataBinding. In ViewModel, I have an AsyncTask callback and it will invoke REST API call. I am getting the response…
20
votes
1 answer

How does Android Paging Library know to load more data?

Iʼm fairly new to developing Android apps and Iʼm trying to do everything “the right way.” So right now, Iʼm implementing the new Android Paging Library into my project, where I need to load a list of articles from a network server. I have an…
20
votes
2 answers

Android ViewModel inside Service (Alternative)

I have a service which provides UI that is visible to user most of the time. I was experimenting with new Application Architecture when I came with a problem. MyModelviewModel viewModel = ViewModelProviders.of(this).get(MyModelviewModel.class); But…
CBeTJlu4ok
  • 1,072
  • 4
  • 18
  • 51
19
votes
3 answers

How do I use the new Saved State Module of ViewModel

I'm using lifecycle version 2.2.0-rc03 and the official docs and articles found don't even list the correct class name or constructor arguments. I think I have to get the ViewModel instance through something like this viewModel =…
Steve M
  • 9,296
  • 11
  • 49
  • 98
19
votes
2 answers

Android Arch Components ViewModel and LiveData trigger after screen rotation

I have a problem when using ViewModel and LiveData arch components. When using fragments and rotating the screen, the observer gets triggered... I tried to move viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java) in all the…
19
votes
5 answers

How to give same instance of ViewModel to both the Parent and Child fragment

There are two Fragments: ParentFragment and ChildFragment. ChildFragment has been added to a view of the ParentFragment. Now using Dagger2 for Android has the ParentFragmentModule with a method: @Provides fun provideViewModel(fragment:…
18
votes
3 answers

Difference between ActivityViewModels and lazy ViewModelProvider?

Difference between ActivityViewModels and lazy ViewModelProvider? I've seen 2 ways to initialize a viewmodel: private val someViewModel: SomeViewModel by activityViewModels() private val someOtherViewModel: SomeOtherViewModel by lazy { …
dbarnes
  • 439
  • 1
  • 3
  • 11
18
votes
1 answer

Android share ViewModel between fragment and dialog fragment?

How to share same viewModel between dialog and dialogFragment? I know that viewModel can be shared in activity scope. But it is too big scope for me. private val model: SharedViewModel by activityViewModels() Unfortunately I don't have in a…
18
votes
4 answers

How to ensure ViewModel#onCleared is called in an Android unit test?

This is my MWE test class, which depends on AndroidX, JUnit 4 and MockK 1.9: class ViewModelOnClearedTest { @Test fun `MyViewModel#onCleared calls Object#function`() = mockkObject(Object) { MyViewModel::class.members …
18
votes
5 answers

How to implement validation using ViewModel and Databinding?

What is the best approach to validate form data using ViewModel and Databinding? I have a simple Sign-Up activity that links binding layout and ViewModel class StartActivity : AppCompatActivity() { private lateinit var binding:…
18
votes
1 answer

Understanding Android Architecture Components example GithubBrowserSample: ViewModelModule, ViewModel parameters

One of the most up to date samples covering Android Architecture Components is GithubBrowserSample provided by Google. I reviewed the code and a few questions arose: I have noticed that ViewModelModule is included in AppModule. It means that all…