Skip to content

Observer is called on load of Fragment Using Navgraph #4

@Mujas

Description

@Mujas

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
})
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions