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
0
votes
0 answers

Two Objects in Live Data referencing each other

I've got two Objects (Object1 and Object2) saved via Android Room, which I want to access using LiveData. Object1 contains a reference to Object2. public class Object1 { ... private Object2 object2; ... } I want to pass a…
0
votes
2 answers

do I have to always call my method in viewmodel from fragment/activity?

so I am new in MVVM pattern in Android. I want to perform two actions, when a button is clicked then first I check the internet connection, if the internet connection is available then perform login to server. here is my ViewModel class…
Alexa289
  • 8,089
  • 10
  • 74
  • 178
0
votes
4 answers

why I can't access my application inside my AndroidViewModel class?

I am new in Android development especially using MVVM pattern. I need context in my viewModel, so I use AndroidViewModel to get context through application, but I don't know why I can't access the application class CreateEventViewModel(application:…
Alexa289
  • 8,089
  • 10
  • 74
  • 178
0
votes
1 answer

Android ViewModel LiveData observe

I'm following ViewModel implementation guidelines as per Android development team provided. However, I'm getting unexpected results. I have been digging the internet but no luck. BookMarketPojo.java class BookMarketPojo { private String…
0
votes
3 answers

How to call notifydatachanged in viewModel MVVM Kotlin?

I have one spinner, I am loading data to that spinner from repository in ViewModel
0
votes
0 answers

Creating ViewModel doubles in android for testing fragments in isolation with espresso

I'm working with android in Java. Our goal is to test a fragment of our app in isolation. Our problem is that we need to setup a view-model double to in order to create meaningful and test worthy fragment state. In particular, the model-view that…
Rotem Barak
  • 154
  • 1
  • 9
0
votes
0 answers

Viewmodel does not change Livedata after first time

I fetch data from Google Books API using Architecture components. This is simple app for search book's authors. The problem is, that when I create ViewModel at first, it observes data , but when I change query to search another book's author, it…
Nika Chapidze
  • 183
  • 1
  • 11
0
votes
0 answers

use executePendingBindings() with gridView

I am trying to use executePendingBinding with gridView adapter. On some data change the binding is not executing at first but other UI operations are being executed before that. How do I prevent that. Here is my ImageAdapter : class ImageAdapter…
0
votes
1 answer

No record save in room database

After inserting data into RoomDB when I fetch it using mindValleyDao.getCategories().value It returns null DatabaseClass @Database(entities = arrayOf(CategoryBO::class), version = 1, exportSchema = false) abstract class MindValleyDatabase :…
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
0
votes
1 answer

how to update textview, ontextchanged in editext using Databinding android

I have edit text which will get input from the user
creativecoder
  • 1,470
  • 1
  • 14
  • 23
0
votes
1 answer

How can I manage fragment and activity considering its creation orders?

What I would like to do I would like to call Activity from Fragment to realize both transitions among fragments and activities simultaneously in an Android app. I have succeeded to call one specific method on activity file from fragment file, but I…
0
votes
1 answer

When getting the ViewModel in a fragment, should I provide "viewModelStore" or "this" as the ViewModelStore to the ViewModelProvider() method

Inside my fragment class, when I am getting my viewModel, I can write my code in two different ways. Using "viewModelStore" ViewModelProvider(viewModelStore, viewModelFactory).get(FragmentViewModel::class.java) Using…
0
votes
2 answers

Android ViewModel observer does not work? Kotlin

Inside the fragment of a tabbed activity: override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) serverSetVM = ViewModelProvider(activity!!).get(ServersViewModel::class.java) serverList…
0
votes
1 answer

One-to-many in Room with Kotlin

The task is to open an activity with notes attached to this diary when you select a single diary. (one-to-many) This is how entities in the database look like: @Entity(tableName = "word_table") data class Word(@ColumnInfo(name = "word") val word:…
0
votes
1 answer

error: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding error(s):?

I am learning databinding with mvvm but I am getting following errors I did not know what is the main problem. DataBinderMapperImpl.java:9: error: cannot find symbol import gahfy.net.databinding.ActivityPostListBindingImpl; …
1 2 3
99
100