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
1 answer

How to stop LiveData observer from firing automatically in Android MVVM architecture?

Consider the following scenario. I have two fragments, FormFragment and SelectionFragment that are hosted by MainActivity. I have a SharedViewModel that I plan on using for data communication between the fragments. FormFragment has a spinner,…
0
votes
2 answers

How to move logic to ViewModel?

How i can move logic from fragment in my ViewModel? override fun onItemClick(titleName: Int) { when (titleName) { R.string.about_terms_service -> { activity?.addFragment( WebViewFragment.newInstance( …
Morozov
  • 4,968
  • 6
  • 39
  • 70
0
votes
1 answer

Viewmodel preserving data after finishing activity

I have a MainActivity form that I am opening CreatePassword Activity in that, I am saving password and finish CreatePasswordActivity with sending Intent back to MainActivity. Like MainActivity -----> CreatePassword(Finish) ---Intent---->…
Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150
0
votes
0 answers

How to avoid subscribe is not used in ViewModel?

For example, I use retrofit in viewModel to make network requests, and now it always prompts The result of subscribe is not used. When I use the viewModel, considering that multiple fragments/activity may use the same viewModel object, I don't want…
Jeong Woo
  • 65
  • 1
  • 9
0
votes
3 answers

How i can add view model in module?

In Dagger, how can I add a model to Module? For example I added the presenter in the following way: @Module class AboutModule(val appContext: Context) { @FragmentScope @Provides fun providePresenter(): AboutListContract.Presenter { …
Morozov
  • 4,968
  • 6
  • 39
  • 70
0
votes
1 answer

Can I use one instead of my two view models?

Can I use one ViewModel instead of my two view models(AboutViewModel and AboutListItemViewModel)? Here is my code: class AboutAdapter(private val clickListener: OnItemClickListener?) : AbstractListAdapter() { override fun…
Morozov
  • 4,968
  • 6
  • 39
  • 70
0
votes
1 answer

Android Display Color is Different From ViewModel Class

I am trying to get color value which has declared in colors.xml and try to get from ViewModel class. In the ViewModel class, color is shown correctly but when I run the app on emulator, the color would be shown differently. I use MutableLiveData…
user13024999
0
votes
1 answer

Changing the value of a string not updating the view in android databinding

I have an object which has a string variable that contains an image url in it. I have a data binding layout that accepts the instance of the previously mentioned object as data and binds the string containing image url to the image view. Until here…
0
votes
1 answer

does edittext retain the data when configuration change ? do I need view model if I only have edittext in my screen?

so I am new in MVVM and in Android in general, and I am little bit confused. it is said that to handle configuration change, I need to use MVVM to avoid data destruction due to configuration change. but it seems that editText can save the data…
0
votes
3 answers

Cannot create instance of ViewModel class

I'm doing this inside my onCreate method of MainActivity journalViewModel = new ViewModelProvider(this).get(JournalViewModel.class); and my ViewModel is as following: public class JournalViewModel extends AndroidViewModel { public…
dkhost07
  • 312
  • 1
  • 4
  • 17
0
votes
1 answer

Share ViewModels between fragments OR share LiveData between ViewModels

Basically I want a way to properly share ViewModels between fragments OR share LiveData between ViewModels. My scenario: I have 2 fragments (FragmentA & FragmentB) - each has its own ViewModels: FragmentA has ViewModelA, FragmentB has…
iadcialim24
  • 3,817
  • 5
  • 21
  • 32
0
votes
3 answers

Understanding Command pattern for navigation from ViewModel

I read a blog article about how to use Android Architecture component in a real application titled Using Navigation Architecture Component in a large banking app In the section How to navigate from ViewModels? however, there is one thing I don't…
0
votes
1 answer

My viewmodel data is not being bound to edittext

I have bounded a viewmodel to an activity and that is working fine, but in another activity where I bound my viewmodel is not showing any data. Here is my viewmodel variable declaration in my activity:
0
votes
0 answers

how to stop observer to automatically observe data when pop back to previous fragment?

I am new in Android MVVM, let say I have 2 screens to show, Fragment A and Fragment B. when user click a button in Fragment A, then I check to server if this user has created an event or not, it he has not created an event then move to Fragment B.…
Alexa289
  • 8,089
  • 10
  • 74
  • 178
0
votes
1 answer

LiveData Observer UninitializedPropertyAccessException: lateinit property siteDto has not been initialized

My app has the following sequence. In the Database layer i have the query that returns LiveData @Query("Select * from sites where server_id = :serverId and site_id = :siteId") fun getSite(serverId: Long, siteId: Int): LiveData In my…
james04
  • 1,580
  • 2
  • 20
  • 46
1 2 3
99
100