android-livedata

Unit testing LiveData observerForever results in NullPointer Exception with Junit5

拟墨画扇 提交于 2020-05-17 07:09:06
问题 I am using Android databinding to listen to live data changes and I would like to observe changes on the viewmodel level (Rather then observing on fragment and then sending a callback to the viewmodel) The observerForever is interesting as it serves the purpose for me. However when I run a test I get the following error: java.lang.NullPointerException at androidx.arch.core.executor.DefaultTaskExecutor.isMainThread(DefaultTaskExecutor.java:77) at androidx.arch.core.executor.ArchTaskExecutor

How to use livedata with Transformations.switchMap correctly to get initial data?

自古美人都是妖i 提交于 2020-05-17 06:06:23
问题 right now I am starting to use LiveData for the first time. First I put all of my code in the viewModel including the code to start a search in the server. I used LiveData like this: Fragment onViewCreated() viewModel.changeNotifierContacts.observe(this, androidx.lifecycle.Observer { value -> value?.let { recyclerViewAdapter.setData(value) } }) This was working as expected. Now I add a repository layer following MVVM pattern. (For this I moved my contact search functionality to repository

Obtain entity using coroutines api

梦想与她 提交于 2020-05-16 08:56:07
问题 What is the best way to use coroutines with LiveData for selecting some data from database using Room . This is My Dao class with suspended selection @Dao interface UserDao { @Query("SELECT * from user_table WHERE id =:id") suspend fun getUser(id: Long): User } Inside of View Model class I load user with viewModelScope . Does it correct way to obtain user entity ? fun load(userId: Long, block: (User?) -> Unit) = viewModelScope.launch { block(database.load(userId)) } According developer

What ViewModelStoreOwner to use for ViewModelProvider in Fragment?

做~自己de王妃 提交于 2020-05-15 07:56:45
问题 I've creating a test activity that updates some text in my MyViewModel. I'd like to observe these changes in a Fragment , but when I use MyViewModel myViewModel = new ViewModelProvider(this).get(MyViewModel.class); it gives me a different instance of MyViewModel than that used in the activity, which results in my onChanged() callback in the fragment not being called. Only when I modify that same fragment code to HomeViewModel homeViewModel = new ViewModelProvider(getActivity()).get

Cannot resolve ViewModelProvider construction in a fragment?

蹲街弑〆低调 提交于 2020-05-13 06:09:27
问题 I have been spending a lot of time trying to figure out why in the code below (towards the end), I get an error on ViewModelProvider(this). I also tried getActivity() instead of 'this', same issue. The error I get is "Cannot resolve constructor ..." import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; public class ItemSetupFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

Cannot resolve ViewModelProvider construction in a fragment?

浪尽此生 提交于 2020-05-13 06:08:07
问题 I have been spending a lot of time trying to figure out why in the code below (towards the end), I get an error on ViewModelProvider(this). I also tried getActivity() instead of 'this', same issue. The error I get is "Cannot resolve constructor ..." import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; public class ItemSetupFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

When to use Android’s LiveData and Observable field?

我的未来我决定 提交于 2020-05-10 15:17:12
问题 I’m implementing a MVVM and data-binding and I’m trying to understand when should I use Observable field over LiveData? I already run through different documentations and discovered that LiveData is lifecycle aware, but in sample codes in Github these two are being used in ViewModel at the same time. So, I’m confused if LiveData is better than Observable field, why not just use LiveData at all? 回答1: Both have their use-cases, for instance: If you want a life-cycle tolerant container for your

ViewModel onchange gets called multiple times when back from Fragment

十年热恋 提交于 2020-05-07 18:25:07
问题 I am working with Android architecture components. What i want is when user type "0" in Edittext and click on Button to replace Fragment with new one , and if type anything else post Toast error message. In Problem is when i back from new Fragment(BlankFragment) and click on button again and type "0" again and click, onchange() is called multiple times so Fragment is get created multiple times FragmentExample.class: @Override public View onCreateView(LayoutInflater inflater, ViewGroup

ViewModel SavedStateHandler.get for LiveData can't set default value as per the document?

萝らか妹 提交于 2020-04-30 04:31:05
问题 In referring to https://developer.android.com/reference/androidx/lifecycle/SavedStateHandle#getLiveData(java.lang.String,%20T) The sample codes in Java as below. String defaultValue = ...; // nullable LiveData<String> liveData; if (defaultValue != null) { liveData = savedStateHandle.get(KEY, defaultValue); } else { liveData = savedStateHandle.get(KEY); } However, I notice that when tried to compile the code, the statement below is not compilable. savedStateHandle.get(KEY, defaultValue); It

How can I make it live data observer working based on user click event with take some user input, but should start observing from onCreate?

爱⌒轻易说出口 提交于 2020-04-18 12:35:22
问题 I have retrofit then repository then view model and then view, but while clicking on button observer don't observe in onChanged. OnClicking on button from onClick, everything is working. I'm getting API response in logcat, but in onChanged it is not called! ManualLicenseKeyViewModel Code: public class ManualLicenseKeyViewModel extends ViewModel { public MutableLiveData<String> key = new MutableLiveData<>(); private MutableLiveData<License> mutableLiveData; private ManualLicenseRepository