Skip to main content
Filter by
Sorted by
Tagged with
264 votes
5 answers
99k views

With the introduction of the Android Architecture Components library, several new classes were introduced, including AndroidViewModel and ViewModel. However, I'm having trouble figuring out the ...
cascal's user avatar
  • 3,059
152 votes
14 answers
143k views

I am working with Single viewModel for the Activity and all of its fragment. So to initialise viewmodel if have to write this setup code in onActivityCreated of all the fragment's override fun ...
Anmol's user avatar
  • 8,670
108 votes
13 answers
83k views

Edit: This question is a bit out of date now that Google has given us the ability to scope ViewModel to navigation graphs. The better approach (rather than trying to clear activity-scoped models) ...
Richard Le Mesurier's user avatar
83 votes
4 answers
32k views

After migrating the Hilt version from 2.33-beta to 2.35 my project has stopped building with the error given below: A txt version: error: cannot access DefaultActivityViewModelFactory class ...
Patryk Kubiak's user avatar
81 votes
5 answers
67k views

I have a fragment: class MyFragment : BaseFragment() { // my StudentsViewModel instance lateinit var viewModel: StudentsViewModel override fun onCreateView(...){ ... } ...
user842225's user avatar
  • 6,119
59 votes
18 answers
74k views

Is it possible to prevent LiveData receive the last value when start observing? I am considering to use LiveData as events. For example events like show message, a navigation event or a dialog ...
Pavel Poley's user avatar
  • 5,633
55 votes
4 answers
36k views

LifecycleOwner is currently needed in order for me to create an observer. I have code which creates an Observer in the ViewModel so I attach the LifecycleOwner when retrieving the ViewModel in my ...
Hiroga Katageri's user avatar
50 votes
2 answers
15k views

In all applications there will always be this three scopes of state: With Compose, a "Per Screen State" could be achieved by: NavHost(navController, startDestination = startRoute) { ... ...
Archie G. Quiñones's user avatar
40 votes
1 answer
26k views

I am very confused due to this new ViewModelProvider api(ViewModelProviders is deprecated) As with the new changes there are new Constructors also (Source code). #1 public ViewModelProvider(@...
Anmol's user avatar
  • 8,670
39 votes
8 answers
36k views

Recently, the class StateFlow was introduced as part of Kotlin coroutines. I'm currently trying it and encountered an issue while trying to unit test my ViewModel. What I want to achieve: testing that ...
agonist_'s user avatar
  • 5,062
33 votes
2 answers
7k views

Recently I've updated my ViewModel to use new viewModelScope. From its implementation, I see that Dispatchers.Main.immediate is set as the default CoroutineDispatcher for viewModelScope. So when ...
Tiko's user avatar
  • 841
32 votes
4 answers
14k views

I'm using the navigation component, I want a view model to be shared between a few fragments but they should be cleared when I leave the fragments (hence not scoping them to the activity) I'm trying ...
martinseal1987's user avatar
26 votes
2 answers
22k views

I want to send an extra parameter to my ViewModel, but this extends from AndroidViewModel. How can I add this parameter to the ViewModelFactory class ? ViewModel class ProjectViewModel(application: ...
mposadar's user avatar
  • 261
26 votes
3 answers
11k views

I have a complex screen in my project which I'm breaking in more than one fragment. I'm trying to follow the MVVM architecture for these classes, so which fragment has its own ViewModel and Contract ...
Igor Escodro's user avatar
  • 1,339
24 votes
3 answers
13k views

I have found a similar question here. At the time of writing this question there is only this answer avaliable, which does not provide any help to me, and I believe also to the person who asked the ...
HelloThere's user avatar
24 votes
7 answers
77k views

I am trying to understand ViewModel and LiveData concepts in android. I am making a practice project but when i added implementation 'androidx.lifecycle:lifecycle-extensions-ktx:2.0.0-alpha1' line in ...
Jaydip Kalkani's user avatar
22 votes
3 answers
25k views

Difference between ActivityViewModels and lazy ViewModelProvider? I've seen 2 ways to initialize a viewmodel: private val someViewModel: SomeViewModel by activityViewModels() private val ...
dbarnes's user avatar
  • 539
21 votes
1 answer
7k views

How to share same viewModel between dialog and dialogFragment? I know that viewModel can be shared in activity scope. But it is too big scope for me. private val model: SharedViewModel by ...
kkkkk's user avatar
  • 746
21 votes
3 answers
25k views

I'm using lifecycle version 2.2.0-rc03 and the official docs and articles found don't even list the correct class name or constructor arguments. I think I have to get the ViewModel instance through ...
Steve M's user avatar
  • 9,860
20 votes
2 answers
12k views

I am trying to use ViewModel in my app. The question comes to my mind is How View Model survives configuration changes. I read number of blog posts saying that " It will create a HolderFragment to ...
FiXiT's user avatar
  • 849
20 votes
2 answers
14k views

I have a service which provides UI that is visible to user most of the time. I was experimenting with new Application Architecture when I came with a problem. MyModelviewModel viewModel = ...
CBeTJlu4ok's user avatar
  • 1,112
19 votes
4 answers
8k views

This is my MWE test class, which depends on AndroidX, JUnit 4 and MockK 1.9: class ViewModelOnClearedTest { @Test fun `MyViewModel#onCleared calls Object#function`() = mockkObject(Object) { ...
Erik's user avatar
  • 4,603
19 votes
2 answers
6k views

I have a problem when using ViewModel and LiveData arch components. When using fragments and rotating the screen, the observer gets triggered... I tried to move viewModel = ViewModelProviders.of(this)....
Nicolas Jafelle's user avatar
18 votes
4 answers
11k views

I have a value in the UI that it's value depends on two LiveData objects. Imagine a shop where you need a subtotal = sum of all items price and a total = subtotal + shipment price. Using ...
Damia Fuentes's user avatar
18 votes
2 answers
6k views

I am trying to inject a ViewModel annotated with @HiltViewModel into a Fragment and get the following error: Injection of an @HiltViewModel class is prohibited since it does not create a ViewModel ...
Pablo Valdes's user avatar
  • 1,024

1
2 3 4 5
38