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
11
votes
2 answers

Jetpack Compose show snack bar from view model - Single Live Event

I'm building a jetpack compose app and I want my view model to tell my compose function to display a snack bar by sending it an event. I have read multiple blog posts about the Single Live Event case with Kotlin and I tried to implement it with…
11
votes
3 answers

How to model parent-child relationship in Android MVVM VMs?

I'm working on an Android piano "quiz" app - users tap on the piano keys and then click the yellow "check" button to submit the answer for evaluation and see the correct answer drawn on the piano. The main QuizActivity has this layout: The upper…
Leprechaun
  • 852
  • 6
  • 25
11
votes
3 answers

ViewModel not getting cleared in Navigation navigate and live data in viewmodel stays alive

So, I have implemented single activity with multiple fragments pattern using Navigation. I used viewmodel for each fragment for non-ui operatios. The problem is when you navigate using findNavController().navigate(), the fragment is not actually…
11
votes
0 answers

Share Viewmodel between BottomSheetDialogFragment with normal Fragment not working (observe function not trigger)

I want to share the same Viewmodel between my base fragment along with opened BottomSheetDialogFragment So this is how i observe to same viewmodel between these two fragment. BottomSheetDialogFragment public class TasteFilterBottomDialogFragment…
Varis Darasirikul
  • 3,907
  • 9
  • 40
  • 75
11
votes
1 answer

Koin - how to provide mock ViewModel for espresso test?

How can we inject mocked viewModel into Activity for espresso test? Using declareMock I get mock object in Test class but Activity receives regular viewModel object. @RunWith(AndroidJUnit4::class) class SomeActivityTest : KoinTest { @Rule …
11
votes
1 answer

Is it a bad practice to have multiple Viewmodels, approximately one for each fragment?

I have a project with 1 main activity and 4 fragments all inheriting from the same "BaseFragment". When first started I managed the whole project with the same ViewModel all through the main activity and fragments but after a while, the code inside…
11
votes
1 answer

MVVM - handling Disposable-s in ViewModel with RxJava and Room

I try to apply the MVVM pattern in my Android activity (I'm an Android noob). I use Room with RxJava 2, e.g. this is a signature of a method in my repository: public Single getMissionTaskByID(long id) {..} In my ViewModel class I have…
Lachezar Balev
  • 11,498
  • 9
  • 49
  • 72
11
votes
2 answers

Android data binding inject ViewModel in custom view

I'm going off this stackoverflow answer https://stackoverflow.com/a/34817565/4052264 to tie a data object to a custom view. However, after everything's set up my view is not being updated with the data and instead the TextView stays blank. Here's my…
11
votes
1 answer

onCleared is not being called on Fragment's attached ViewModel

I stumbled on a problem when ViewModel.OnCleared() is not being called when the app goes to background (even if Don't keep activities is enabled) but I can see that Fragment.onDestroy() is actually being called. What could be wrong in the following…
11
votes
1 answer

Android Architecture Component ViewModel - How to mock ViewModel on test Activity?

I'm trying to setup UI testing similar to GithubBrowserSample and it looks like the sample project only has mock ViewModel for Fragment but not an example for Activity. Here's my code where I am trying to test the Activity by mocking ViewModel. But…
10
votes
1 answer

How could i run my logic code inside composable function just 1 time?

I'm doing my test project using ViewModel and ComposeView. My architecture include: one Activity and multi ComposeView, using navigation like this: class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { …
10
votes
1 answer

viewModelScope.launch does not work for second time

I have 2 fragments, FragmentA contains a list of starwar characters whereas FragmentB contains details of that character. I am using viewModelScope.launch in my fragments to fetch details for a character. Below is my ViewModel @HiltViewModel class…
10
votes
2 answers

Jetpack Compose saving state on orientation change

I am using Android Jetpack's Compose and have been trying to figure out how to save state for orientation changes. My train of thought was making a class a ViewModel. As that generally worked when I would work with Android's traditional API. I have…
10
votes
2 answers

ViewPager2/Tabs problem with ViewModel state

I am following the MVVM pattern - meaning I have a ViewModel for each Fragment. I added two tabs by using ViewPager2. My adapter looks like this: @Override public Fragment createFragment(int position) { switch (position) { case 0: …
10
votes
2 answers

LiveData: Remove observer from inside Observer lambda

Inside my fragment i'm observe a livedata: viewModel.emailValid.observe( this, Observer { dismissProgressBar() if (it != null && it.success) { …
giozh
  • 9,868
  • 30
  • 102
  • 183