Skip to content

Commit

Permalink
Databinding: use viewLifecycleOwner instead of Fragment as lifecycle …
Browse files Browse the repository at this point in the history
…owner
  • Loading branch information
johan12345 committed Oct 26, 2024
1 parent c90f32f commit cafc477
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ class ChargepriceFragment : Fragment() {
inflater,
R.layout.fragment_chargeprice_header, container, false
)
binding.lifecycleOwner = this
binding.lifecycleOwner = viewLifecycleOwner
binding.vm = vm
headerBinding.lifecycleOwner = this
headerBinding.lifecycleOwner = viewLifecycleOwner
headerBinding.vm = vm

binding.toolbar.inflateMenu(R.menu.chargeprice)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class FavoritesFragment : Fragment() {
inflater,
R.layout.fragment_favorites, container, false
)
binding.lifecycleOwner = this
binding.lifecycleOwner = viewLifecycleOwner
binding.vm = vm

return binding.root
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class FilterFragment : Fragment(), MenuProvider {
savedInstanceState: Bundle?
): View {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_filter, container, false)
binding.lifecycleOwner = this
binding.lifecycleOwner = viewLifecycleOwner
binding.vm = vm
vm.filterProfile.observe(viewLifecycleOwner) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class FilterProfilesFragment : Fragment() {
savedInstanceState: Bundle?
): View {
binding = FragmentFilterProfilesBinding.inflate(inflater, container, false)
binding.lifecycleOwner = this
binding.lifecycleOwner = viewLifecycleOwner
binding.vm = vm

return binding.root
Expand Down
11 changes: 7 additions & 4 deletions app/src/main/java/net/vonforst/evmap/fragment/MapFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class MapFragment : Fragment(), OnMapReadyCallback, MapsActivity.FragmentCallbac
): View {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_map, container, false)
println(binding.detailView.sourceButton)
binding.lifecycleOwner = this
binding.lifecycleOwner = viewLifecycleOwner
binding.vm = vm

val provider = prefs.mapProvider
Expand Down Expand Up @@ -1021,7 +1021,8 @@ class MapFragment : Fragment(), OnMapReadyCallback, MapsActivity.FragmentCallbac
}
}
vm.mapPosition.observe(viewLifecycleOwner) {
binding.scaleView.update(map.cameraPosition.zoom, map.cameraPosition.target.latitude)
val target = map.cameraPosition.target ?: return@observe
binding.scaleView.update(map.cameraPosition.zoom, target.latitude)
}

map.setOnCameraMoveStartedListener { reason ->
Expand Down Expand Up @@ -1372,8 +1373,10 @@ class MapFragment : Fragment(), OnMapReadyCallback, MapsActivity.FragmentCallbac
}
}

override fun onDestroy() {
super.onDestroy()
override fun onDestroyView() {
super.onDestroyView()
map = null
vm.mapProjection = null
/* if we don't dismiss the popup menu, it will be recreated in some cases
(split-screen mode) and then have references to a destroyed fragment. */
popupMenu?.dismiss()
Expand Down

0 comments on commit cafc477

Please sign in to comment.