Category : android-livedata

My ActivityA has FragmentA which has a viewModel scoped to its parent and I observe a livedata to update my UI as below – class FragmentA: Fragment(){ private lateinit var viewModel: MyViewmodel private lateinit var textView:TextView override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { // Inflate the layout for this fragment ..

Read more

How do you launch a fragment in the test scope that contains a LiveData observer with Robolectric. My fragment is observing the liveData from the ViewModel. If I remove this section of code the test runs. fun observerData() { viewModel.MyLiveData.observe(viewLifecycleOwner, Observer { … }) } The test uses a fragment scenario to start the fragment. ..

Read more

How to test a LiveData observer in the scope test. I have a fragment that is testing observing LiveData, for simplicity I’m showing the function with the observer. fun liveDataObserver() { viewModel.scoreLiveData.observe(viewLifecycleOwner, { Log.i("Practice", "New score is $it") } ) } I use Robolectric to launch the fragment in the test scope. @RunWith(RobolectricTestRunner::class) class ScoreKeeperFragmentTest ..

Read more

I would like to have an structure like the following one: CustomActiviy -> MyAppActivity -> AppCompatActivity CustomViewModel -> MyAppViewModel -> ViewModel (Android one) in order to have a couple of MutableLiveData objects in the MyAppViewModel (one related to showing toast messages and the other to switch a progress bar) and observe them in the MyAppActivity. ..

Read more