Questions tagged [android-mvvm]

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

773 questions
6
votes
2 answers

Update a Record if exists, else insert a new record in Room

I am new to Room, Rxjava and other android architecture components. I am trying to update/insert 2 records (If row already exists,update it. otherwise insert a new row.) I have tried to do it in following way. But, I didn't…
Riddhi Shah
  • 3,092
  • 5
  • 36
  • 56
6
votes
3 answers

Android ViewModel recreated when its Host Activity was not in the top of Activity Stack and the device was rotated

I am in the following scenario: I have an OnboardActivity which contains a ViewModel, I can rotate this OnboardActivity many times and the ViewModel persist across configuration changes without issues. However, if I launch another…
5
votes
2 answers

MVVM - Having a hard time understanding how to create the Domain layer in Clean Architecture

I'm trying to learn MVVM to make my app's architecture more clean. But I'm having a hard time grasping how to create a "Domain" layer for my app. Currently this is how the structure of my project is looking: My View is the activity. My ViewModel has…
DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83
5
votes
0 answers

MVVM communication between Service and ViewModel

I have a Service that loads data from via http request every second. I am wondering how to implement the service in my current MVVM structure. A solution I could think of is to: View calls method from ViewModel ViewModel calls method from…
5
votes
3 answers

Cannot create an instance of class of AndroidViewModel in android

I am trying to add Room database with Android ViewModel. I followed this link for the same https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#13 I am getting an exception : Caused by: java.lang.RuntimeException: Cannot create…
Sid
  • 2,792
  • 9
  • 55
  • 111
5
votes
1 answer

Where to make API's requests while using MVVM architecture in android?

I am using MVVM architecture in my application and i want to make API requests from the activity's viewmodel class. The problem here is I am not getting the best way to do so. As viewmodel is already lifecycle aware for that activity so there is no…
Cosmic Dev
  • 522
  • 6
  • 20
5
votes
2 answers

How to write a use case that retrieves data from Android framework with Context

I am migrating an application to MVVM and clean architecture, and I am missing one part of the puzzle. The problem domain: List all applications on device and display them in the Fragment / Activity A device app is represented by its package…
BennyP
  • 1,737
  • 1
  • 18
  • 24
5
votes
3 answers

MVVM- Recyclerview inside included layout is never initialized

I have a generic list that need to be show on most of the activities/fragments throughout my app. For this I have created a layout and added the recycler in it as in below code. This layout is then included in the activities/fragment where required.…
5
votes
5 answers

Android ViewModel observer not called from background

I'm working on small android app using MVVM pattern. My issue is that my ViewModel observer in MyActivity not called from the background. I need it to be called even if the app is in background to show system Notification to the user that app…
iluxa.b
  • 375
  • 1
  • 6
  • 16
5
votes
1 answer

How to update live data of the viewModel, when click is performed on recyclerview item

I am new to the Kotlin and I am trying to create an application with MVVM. so what I am trying to do is to make a web call using retrofit in the repository and returning data to the view model and then observing the data from the fragment and as…
5
votes
0 answers

Not able to import InstantTaskExecutorRule in my 'jUnit' test case even after adding 'core-testing' dependency - Android testing

I am writing test cases for my LoginViewModel. Where I want to perform setValue() operation on my MutableLiveData. To avoid Method getMainLooper in android.os.Looper not mocked Exception I am trying to add the following Rule in my ViewModel file…
5
votes
1 answer

How to highlight recyclerview item when using MVVM architecture and databinding?

I'm trying to highlight a recyclerview item while adhering to MVVM principles and while using databinding. But I'm having trouble understanding how to handle selecting a row. I currently pass my recyclerview item onclick to the viewmodel using the…
5
votes
1 answer

Updating a single item in a recylerview. I am using paging library and would like to update a single item/row

I am passing pagedlist values to adapter using submit list. when i update a single item consider i am clicking a like button of a feed in a recyclerview. how to update the single item. i am following this example for paging…
5
votes
1 answer

StartActivityForResult with MVVM Architecture Components

In one of the Google Codelabs, Activity1 displays a RecyclerView and on button click opens Activity2 where a new entry can be added. Instead of accessing a ViewModel in Activity2 and adding the entry to the database directly, they send the entry…
5
votes
1 answer

How to make this ViewModelFactory more flexible and accept different kinds of ViewModel classes?

I copied an example of MVVM with Android Architecture Components, Retrofit, Dagger, and data binding. I am using this code as a starting point to my app in order to start using better architectures in Android app development. However, take these…
user4383363