Questions tagged [android-mvvm]

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

773 questions
7
votes
1 answer

Android viewmodel unit test with livedata

I am unit testing my viewmodel and I always keep getting NullPointerException. Here is my viewmodel code - class LoginViewModel(private val myUseCase: MyUseCase) :BaseViewModel() { private val viewState = LoginViewState() fun…
Ma2340
  • 647
  • 2
  • 17
  • 34
7
votes
2 answers

MutableLiveData retains old value even when the observer activity gets destroyed

I have an activity which is observing livedata from repository. Now when the activity gets destroyed and then created again, i still get the old value from repository unless i fetch the new one manually. Why is the mutablelivedata retaining its old…
7
votes
4 answers

Let every Observer only receive *new* LiveData upon subscribing/observing

Whenever you call .observe() on LiveData, the Observer receives the last value of that LiveData. This may be useful in some cases, but not in mine. Whenever I call .observe(), I want the Observer to receive only future LiveData changes, but not…
7
votes
2 answers

MVVM in android,accessing assetManager without breaking the pattern

I have a JSON file in the assets folder and DataManager(repository) class needs it so assetManager(and context) should have access to the assets. The problem is that based on Best practice, Android context or android specific code should not be…
Hessam-Emami
  • 347
  • 2
  • 4
  • 15
7
votes
0 answers

Mock ViewModel in MVVM Activity

Update 29/04/18 Re-titled for better accuracy. Problem is simple: ViewModels cannot be simply mocked on Activities because they are instantiated in an Acitivity's onCreate(). What is the best way around this? Some related ideas located here…
7
votes
1 answer

Using android two way databinding with checkbox

I'm trying to use android two-way databinding with checkBox in a fragment. I have multiple fragments in viewPager with custom FragmentStatePagerAdapter and one viewModel in first fragment. I have a checkbox with the code below:
Sepehr Nozaryian
  • 370
  • 4
  • 15
7
votes
5 answers

LiveData Object keeps being null after getValue() is called

I want to update a member variable of an object inside my Repository on a LiveData- Object. The problem is, that if I call the getValue() Method, I keep getting an NullPointerException, although the value does exist inside my Room- Library. My…
romaneso
  • 1,097
  • 1
  • 10
  • 26
7
votes
1 answer

MVVM Dagger2 A Binding With Matching Key Exists in Component

I am using the following google sample project: https://github.com/googlesamples/android-architecture-components as a reference for my new project and having difficulties trying to add a second activity to the project. Here is the error I get when…
6
votes
3 answers

Shared view model in android compose project while using Hilt for dependency injection?

Anyone has any clue how to retain a shared view model object across different composables? I'm using hilt and injecting viewmodel instance using hilt in composable. Basically there are 3 screens which share same data and changes I want to share it…
Ali Nawaz
  • 2,016
  • 20
  • 30
6
votes
1 answer

Difference between by viewmodels and viewmodel creation using Factory?

I am studying ViewModel to apply it to MVVM design pattern. There was a method using by viemodels() and a method using ViewModelProvider.Factory in view model creation. by viewModels() creates a ViewModel object. ViewModelProvider.Factory also…
ybybyb
  • 1,385
  • 1
  • 12
  • 33
6
votes
3 answers

RxJava + Websocket - How to add Observable to Websocket listener?

I have a ViewModel that is observing a RxJava Observable in my MainRepo class. I am trying to get my WebsocketListener in the MainRepo class to emit events, but I'm unsure how to do so. MainRepo class: private WebSocket ws; public void…
DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83
6
votes
2 answers

Navigate from one fragment to another when using MVVM pattern for Android

I am creating an app using MVVM pattern.I am using Navigation Graph to manage fragments in my app and as per the recommended approach we don't have to put UI logic inside Activity/Fragments but in Viewmodel . So my question is how to navigate from…
Sumit Shukla
  • 4,116
  • 5
  • 38
  • 57
6
votes
2 answers

How to set OnClickListener on RecyclerView item in MVVM structure

I have an app structured in MVVM. I have different fragments within the same activity. Each fragment has its own ViewModel and all data are retrieved from a REST API. In FragmentA, there is a RecyclerView that lists X class instances. I want to set…
Mehmed
  • 2,880
  • 4
  • 41
  • 62
6
votes
0 answers

RxJava with MVVM: MutableLiveData w/ setValue() vs. LiveDataReactiveStreams

Let's say you have a MVVM app with a UI layer, a ViewModel, and a Repository. Say that in your repository, you're getting some data from an API with Single Retrofit calls, and transforming it into a UI-ready viewstate object. The way I see it, you…
ajh3
  • 101
  • 1
  • 4
6
votes
1 answer

How to implement Livedata and Databinding in Android?

I searched a lot about observable models and parameters but I got confused! 1-some of model extend from BaseObservable and setnotifyChange(); in setters and bind them for example to edittext like this:…