Hello,
Could you add an example instrumentation tests which uses:
- navGraphViewModels
- FragmentScenario
- launchFragmentInHiltContainer
- databinding where the nav graph scoped viewmodel is set into the viewbinding
I cannot get this combination to work in an instrumentation test because I cannot set the navcontroller early enough. It is still null when onViewCreated is called. When viewBinding.viewModel = myViewModel is done in the code snippet below, findNavController will eventually be called but I have not been able to set it via Navigation.setViewNavController(...)
Fragment contains
private val myViewModel: MyViewModel by navGraphViewModels(R.id.my_nav_graph) { defaultViewModelProviderFactory }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) /* Sets the {@link LifecycleOwner} that should be used for observing changes of * LiveData in this binding. If a {@link LiveData} is in one of the binding expressions * and no LifecycleOwner is set, the LiveData will not be observed and updates to it * will not be propagated to the UI. */ viewBinding.lifecycleOwner = this viewBinding.viewModel = myViewModel // pass in the view model }
Hello,
Could you add an example instrumentation tests which uses:
I cannot get this combination to work in an instrumentation test because I cannot set the navcontroller early enough. It is still null when onViewCreated is called. When viewBinding.viewModel = myViewModel is done in the code snippet below, findNavController will eventually be called but I have not been able to set it via Navigation.setViewNavController(...)
Fragment contains
private val myViewModel: MyViewModel by navGraphViewModels(R.id.my_nav_graph) { defaultViewModelProviderFactory }override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) /* Sets the {@link LifecycleOwner} that should be used for observing changes of * LiveData in this binding. If a {@link LiveData} is in one of the binding expressions * and no LifecycleOwner is set, the LiveData will not be observed and updates to it * will not be propagated to the UI. */ viewBinding.lifecycleOwner = this viewBinding.viewModel = myViewModel // pass in the view model }