1,860 questions
264
votes
5
answers
99k
views
AndroidViewModel vs ViewModel
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 ...
152
votes
14
answers
143k
views
how to get viewModel by viewModels? (fragment-ktx)
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 ...
108
votes
13
answers
83k
views
Manually clearing an Android ViewModel?
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) ...
83
votes
4
answers
32k
views
DefaultActivityViewModelFactory not found
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 ...
81
votes
5
answers
67k
views
Use viewLifecycleOwner as the LifecycleOwner
I have a fragment:
class MyFragment : BaseFragment() {
// my StudentsViewModel instance
lateinit var viewModel: StudentsViewModel
override fun onCreateView(...){
...
}
...
59
votes
18
answers
74k
views
LiveData prevent receive the last value when start observing
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 ...
55
votes
4
answers
36k
views
Should I include LifecycleOwner in ViewModel?
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 ...
50
votes
2
answers
15k
views
Scoping States in Jetpack Compose
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) {
...
...
40
votes
1
answer
26k
views
What is ViewModelStore and viewModelStoreOwner?
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(@...
39
votes
8
answers
36k
views
Unit test the new Kotlin coroutine StateFlow
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 ...
33
votes
2
answers
7k
views
Retrofit call in Kotlin Coroutines viewModelScope
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 ...
32
votes
4
answers
14k
views
Scoping a viewmodel to multiple fragments (not activity) using the navigation component
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 ...
26
votes
2
answers
22k
views
How to use a ViewModelProvider.Factory when extends from AndroidViewModel
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: ...
26
votes
3
answers
11k
views
Sharing Data between ViewModels
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 ...
24
votes
3
answers
13k
views
How to use Hilt to inject a safe-args argument into a viewmodel?
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 ...
24
votes
7
answers
77k
views
Failed to resolve: androidx.lifecycle:lifecycle-extensions-ktx:2.0.0-alpha1
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 ...
22
votes
3
answers
25k
views
Difference between ActivityViewModels and lazy ViewModelProvider?
Difference between ActivityViewModels and lazy ViewModelProvider?
I've seen 2 ways to initialize a viewmodel:
private val someViewModel: SomeViewModel by activityViewModels()
private val ...
21
votes
1
answer
7k
views
Android share ViewModel between fragment and dialog fragment?
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 ...
21
votes
3
answers
25k
views
How do I use the new Saved State Module of ViewModel
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 ...
20
votes
2
answers
12k
views
How ViewModel survives configuration change
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 ...
20
votes
2
answers
14k
views
Android ViewModel inside Service (Alternative)
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 = ...
19
votes
4
answers
8k
views
How to ensure ViewModel#onCleared is called in an Android unit test?
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) {
...
19
votes
2
answers
6k
views
Android Arch Components ViewModel and LiveData trigger after screen rotation
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)....
18
votes
4
answers
11k
views
LiveData Transformations.map() with multiple arguments
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 ...
18
votes
2
answers
6k
views
Injection of an @HiltViewModel class is prohibited since it does not create a ViewModel instance correctly
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 ...