Questions regarding applying a Model-View-ViewModel in android application
Questions tagged [android-mvvm]
773 questions
2
votes
0 answers
Testing Flow with MVVM result NullPointerException
I am trying to test the return value of a ViewModel method. When the method is called, a usecase is executed and return a flow. Then the method return a liveData from the flow:
The viewmodel:
class HomeDataViewModel
@Inject constructor(private val…

esteban
- 543
- 4
- 18
2
votes
1 answer
What Context should i pass on WorkManager.getInstance().enqueue() ? (Using MVVM + Room Database + WorkManager)
I am trying to enqueue a WorkManager worker through a repository Method in order to insert some data inside my database but i can't seem to figure out what type of context i should pass inside the getInstance(). Obviously the Context i am giving is…

George Sinis
- 43
- 5
2
votes
3 answers
"Cannot access database on the main thread since it may potentially lock the UI for a long period of time" error on lauching coroutine
I get this error: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
It happens when I launch fun turnAllWordsOn() in the ViewModel (code below). This function launches coroutine, and I thought…

Alex Rodionow
- 247
- 3
- 13
2
votes
1 answer
java.lang.ClassCastException: com.eng.bt_eng_mobility.viewmodel.HomeViewModelFactory cannot be cast to androidx.lifecycle.ViewModel
I am getting this error. I have created ViewModelFactory class inside same ViewModel class file.
When Im trying to initialise the viewmodel I am getting this error.
//Code written in fragment class in onCreateView after binding…

Josss
- 229
- 1
- 4
- 19
2
votes
1 answer
MVVM - How to share a single repository class across multiple ViewModels
I have multiple viewmodels that access a single repository (one activity and the rest fragments).
AdminActivityViewModel
AdminListUsersViewModel
AdminUserTransactionsViewModel
... and a few more
My AdminRepo class has multiple constructors so that I…

DIRTY DAVE
- 2,523
- 2
- 20
- 83
2
votes
4 answers
BaseFragment with viewbinding
To use viewbinding in an android app I am basically creating base classes for Activity & Fragment to remove boilerplate of everytime writing inflating code.
ACTIVITY:
BaseActivity with viewbinding:
abstract class BaseActivity :…

Sumit Shukla
- 4,116
- 5
- 38
- 57
2
votes
0 answers
Performace overhead when creating too many mutable livedata in a viewmodel
I wanted to know if there could be any performance issue if we create too many mutable live data objects in single viewmodel? in recent project I saw too many mutable live data objects in a viewmodel which I saw first time and made question…

blackHawk
- 6,047
- 13
- 57
- 100
2
votes
1 answer
Android MVVM with servicelocator
Im trying to make mvvm pattern with repository and servicelocator to use mock's or remote calls, it depends on flavour.
What happening now,is that my liveData is not updating after i receive response from server. So for now i always have a empty…

user1851366
- 256
- 6
- 20
- 41
2
votes
1 answer
How does Android Room DAO @Insert with return value(long or int) work?
Recently I'm learning to use DAO. From what I understand, all the @Insert, @Update, or @Query are executed asynchronously. And from the documentary, @Insert can return a long value, which is the new rowId for the inserted item (or List if…

JosephW
- 253
- 1
- 2
- 10
2
votes
3 answers
ViewModelProvider is causing the crash as soon as fragment is opened. How do I resolve this?
I am making a note app with Room Database but the line noteViewModel = ViewModelProvider(this).get(NoteViewModel::class.java) in AddFragment.kt is making my app crash as soon as the AddFragment is opened. To view the full code find the github…

Himanshu013
- 53
- 4
2
votes
0 answers
Unable to send current city name to the API
I am fetching weather data from an API. The problem that I am facing is when I get the cityName in textView from the current locations and then I have to send that cityName to the API so I can get the temperature of that city. But I am unable to do…

Bhavya Varmora
- 90
- 5
2
votes
1 answer
Android: Impossible NullPointerException when using viewModelscope and withContext
My problem is, that I get an impossible NullPointerException. When I access my emailEntity data from my price variable without using an elvis-operator, my price variable gets null and I get a NullPointerException.
Now comes the problem: When I use…

Andrew
- 4,264
- 1
- 21
- 65
2
votes
1 answer
Validate edittext input using databinding library
I am using databinding library. I have a layout which have some edittext for getting basic user input like name, email, password etc. I want to validate these inputs inside the viewmodel on button click. I am a bit confused how to access edittext…

Andrain
- 872
- 1
- 16
- 43
2
votes
2 answers
Android my bottom app a bar is overlapping my recyclerview
I am trying to fix this issue where my recyclerview is overlapped by bottomappbar. Here is my layout code .

NinjaXX2
- 43
- 5
2
votes
2 answers
ViewModel Problem to show Database changes in M V VM Architecture
In my try to approach to M V VM Architecture I have made this simple Example, and my problem is viewModel
don`t show changes that I made to Database, until I restart my app.
1-this is my model class:
@Entity
public class Product {
@ColumnInfo
…

Hadi
- 124
- 9