Navigation-Components-Example icon indicating copy to clipboard operation
Navigation-Components-Example copied to clipboard

Observer is called on load of Fragment Using Navgraph

Open Mujas opened this issue 5 years ago • 0 comments

Known issue in all blogs.

I have Fragment A and Fragment B, called Fragment B and inside this i have one api call and I am observing the Livedata. I got the response from API and its working fine. I navigateUp to Fragment A and come back again to Fragment B, again the last observer trigers. Even though am removing the observer and creating new.

Fragment B:

private val viewModel : MyViewModel by activityViewModels() override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) viewModel .getData().removeObservers(viewLifecycleOwner) viewModel .getData().observe(viewLifecycleOwner, Observer {
navController.navigateUp() // this calls again and again }) }

Mujas avatar Aug 07 '20 13:08 Mujas