Questions tagged [android-mvvm]

Questions regarding applying a Model-View-ViewModel in android application

773 questions
31
votes
4 answers

Using LiveData with Data Binding

With the stabilization of Android Architecture Components I started updating all my basic ViewModels to the new implementation ofViewModel. In my understanding, the usage of LiveData is recommended to hold the Model class since it handles the…
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
22
votes
2 answers

Android: MVVM is it possible to display a message (toast/snackbar etc.) from the ViewModel

I want to know what is the best approach to display some sort of message in the view from the ViewModel. My ViewModel is making a POST call and "onResult" I want to pop up a message to the user containing a certain message. This is my…
user3182266
  • 1,270
  • 4
  • 23
  • 49
21
votes
2 answers

Android unit testing view model that receives flow

I have a ViewModel that talks to a use case and gets a flow back i.e Flow. I want to unit test my ViewModel. I am new to using the flow. Need help pls. Here is the viewModel below - class MyViewModel(private val handle: SavedStateHandle,…
Ma2340
  • 647
  • 2
  • 17
  • 34
21
votes
1 answer

How to access SharedPreferences in a ViewModel?

I am developing an Android application using MVVM. I need to access SharedPreferences in a ViewModel, but I do not know how to do it. I know that it is possible to access a context when inheriting AndroidViewModel, but I want to know if it is…
Tlaloc-ES
  • 4,825
  • 7
  • 38
  • 84
18
votes
4 answers

Cannot resolve ViewModelProvider construction in a fragment?

I have been spending a lot of time trying to figure out why in the code below (towards the end), I get an error on ViewModelProvider(this). I also tried getActivity() instead of 'this', same issue. The error I get is "Cannot resolve constructor…
18
votes
3 answers

Android MVVM - How to make LiveData emits the data it has (forcing to trigger the observer)

I have this ViewModel that gets a list from the network and I populate a RecyclerView with the data (MyAvailabilityRepository returns a MutableLiveData, that's why i'm using Transformations.switchMap): class MyAvailabilityViewModel : ViewModel() { …
16
votes
4 answers

Handle Toolbar back button with Navigation component

I'm following single activity approach. I have navigation toolbar, whenever i go to other screens (fragments) instead of hamburger icon i will have back arrow. What i want to achieve is, pop my current fragment using action on pressing toolbar back…
Shahal
  • 1,008
  • 1
  • 11
  • 29
16
votes
5 answers

Unable to get provider androidx.lifecycle.ProcessLifecycleOwnerInitializr

Getting error while running app which have dependencies 'androidx.lifecycle:lifecycle-extensions:2.1.0-alpha03' and 'androidx.lifecycle:lifecycle-viewmodel:2.1.0-alpha03' on device with Android 4.4. Works fine in device with Android 6 App getting…
Ajay Simiyon
  • 461
  • 1
  • 4
  • 9
16
votes
2 answers

What are the differences between ViewModelProvider.Factory and ViewModelProvider.NewInstanceFactory?

I'm currently transforming my project architecture from MVP to MVVM. When I'm working on it, I find something made me confused: In ScheduleViewModelFactory.kt of project iosched, the factory implements ViewModelProvider.Factory: class…
15
votes
1 answer

Get java.lang.RuntimeException: Failed to call observer method from binding implementation method

please help me with my problem. I'd like to get LiveData from my DataBase and and display it with recycleview. I have method in Repository to get it from database: fun getEthTransactions(): LiveData> { return…
15
votes
6 answers

Make certain part of text bold using DataBinding

I want to make certain part of my text to bold whose value is set using DataBinding with ViewModel. For e.g If you are selected, you will pay $160 for your pair. I am using strings resources If you are selected, you…
Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
15
votes
1 answer

Should we create a separate repository for each Activity or single repository for entire app

I am following MVVM architecture for my app. In Activity I need to getNews() from a URL, if news is not already cached. Activity will ask ViewModel to give the News Json and ViewModel will look into the repository, and decide to send data from local…
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
4 answers

Learn Android MVVM architecture components with kotlin

After searching about android architecture components I realized that MVVM is better than other architectures at some points so I started to learn MVVM and I'm using Kotlin language but the problem is when I search for tutorials to start learning I…
1
2 3
51 52