Skip to content

Commit

Permalink
Fixed multiple observers alive
Browse files Browse the repository at this point in the history
android/architecture-components-samples#47
As pointed out in that blogpost, everything's fine if a fragment is
destroyed (after a rotation for example), however if it's
detached/attached then the observer isn't removed and a new one is
created every time - unless we use `viewLifecycleOwner` instead of
`this` as the observation's owner.
  • Loading branch information
Marcelo Gobetti committed Sep 3, 2018
1 parent 5fe1bd8 commit 6d60172
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ListFragment : Fragment(), OpenDetailsListener {
recyclerView.adapter = moviesAdapter
recyclerView.layoutManager = LinearLayoutManager(context)

viewModel.movies.observe(this, Observer<PagedList<Movie>> {
viewModel.movies.observe(viewLifecycleOwner, Observer<PagedList<Movie>> {
moviesAdapter.submitList(it)
})
}
Expand Down

0 comments on commit 6d60172

Please sign in to comment.