android-viewmodel

can I make a livedata observer in my viewmodel? or should I always observer in fragment/activity?

自作多情 提交于 2020-04-18 05:29:40
问题 I am new to MVVM. so I have 2 requests to the server from my fragment/activity, the result from the first request will be used as an input parameter for the second request. so first in my fragment, when a button is clicked then I make a request to check whether the user is banned or not, if not then this user can create a post. so first I check if a user is banned or not using this code in my fragment class CreateEventFragment : Fragment() { lateinit var model: CreateEventViewModel override

Share ViewModels between fragments OR share LiveData between ViewModels

一个人想着一个人 提交于 2020-04-18 03:50:58
问题 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 ViewModelB . ViewModelA has LiveDataA1 , ViewModelB has LiveDataB1 and LiveDataB2 ViewModelB is only allowed to have LiveDataB2 and ViewModelA cannot have it. Problem is I want FragmentA to observe LiveDataB2 from ViewModelB . Approach#1: Aside from

AndroidViewModel with SavedState

心不动则不痛 提交于 2020-04-16 02:47:23
问题 I need to use an AndroidViewModel with application context and a SavedStateHandle . I have it already working with application context, but I fail adding a SavedStateHandle to it. This is what I have, with only the application context: // A1. get ViewModel in Fragment val viewModel = ViewModelProvider(viewLifecycleOwner).get(MyViewModel::class.java) // A2. MyViewModel derives from my custom BaseAndroidViewModel class MyViewModel(application: Application) :BaseAndroidViewModel(application) //

How to use a ViewModelProvider.Factory when extends from AndroidViewModel

99封情书 提交于 2020-04-06 02:34:19
问题 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: Application) : AndroidViewModel(application) { // need a param for project id... } ViewModelFactory class ProjectViewModelFactory(val projectId: Int): ViewModelProvider.Factory { override fun <T : ViewModel?> create(modelClass: Class<T>): T { // need to send this... return ProjectViewModel

Android : How to write a unit test for fragment depending on a viewmodel Live data attribute?

怎甘沉沦 提交于 2020-03-25 18:57:08
问题 I have a listview in my fragment UI that its elements set depend on status of a value that come from a viewmodel LiveData attribute. I want to create instrumental test for the fragment which englobes 3 scenarios test case related to the value set of that attribute and I don't where to start. My code should kind look like below : class MyViewModel : ViewModel() { var status = MutableLiveData("") } class MyFragment : Fragment() { private lateinit var myViewModel: MyViewModel private lateinit

Do we really need viewModelFactories and viewmodelProviders when using Dagger?

橙三吉。 提交于 2020-03-05 03:11:08
问题 So I was working on some sample MVVM project using Dagger. I have a viewmodel factory that goes like this: class DaggerViewModelFactory @Inject constructor(private val viewModelsMap: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>) : ViewModelProvider.Factory { override fun <T : ViewModel> create(modelClass: Class<T>): T { val creator = viewModelsMap[modelClass] ?: viewModelsMap.asIterable().firstOrNull { modelClass.isAssignableFrom(it.key) }?.value ?: throw

ViewPager2/Tabs problem with ViewModel state

ぐ巨炮叔叔 提交于 2020-02-29 13:23:23
问题 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: return new MergedItemsFragment(); case 1: return new ValidatedMergedItemsFragment(); } return new MergedItemsFragment(); } The tabs are working. However, I noticed that the ViewModel of my MergedItemsFragment is behaving weirdly. Before I added tabs I navigated to

ViewPager2/Tabs problem with ViewModel state

浪尽此生 提交于 2020-02-29 13:21:13
问题 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: return new MergedItemsFragment(); case 1: return new ValidatedMergedItemsFragment(); } return new MergedItemsFragment(); } The tabs are working. However, I noticed that the ViewModel of my MergedItemsFragment is behaving weirdly. Before I added tabs I navigated to

ViewPager2/Tabs problem with ViewModel state

ε祈祈猫儿з 提交于 2020-02-29 13:20:48
问题 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: return new MergedItemsFragment(); case 1: return new ValidatedMergedItemsFragment(); } return new MergedItemsFragment(); } The tabs are working. However, I noticed that the ViewModel of my MergedItemsFragment is behaving weirdly. Before I added tabs I navigated to

injecting viewmodel with navigation-graph scope: NavController is not available before onCreate()

梦想的初衷 提交于 2020-02-15 00:22:42
问题 I'm using a navigation-component in my application and also using shared ViewModel between multiple fragments that are in the same graph. Now I want to instantiate the ViewModel with this graph scope with this. As you know, in fragments we should inject objects ( ViewModel,..etc ) in onAttach: but when I want to do this (injecting ViewModel with a graph scope in onAttach ), this error occurs: IllegalStateException: NavController is not available before onCreate() Do you how can I do this? 回答1