Issue
Which approach is correct:
Passing
Fragment
instance aslifecycleOwner
:override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { val binding = FragmentEventSettingsBinding.inflate(inflater, container, false) binding.lifecycleOwner = this return binding.root }
Passing
viewLifecycleOwner
of theFragment
:override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { val binding = FragmentEventSettingsBinding.inflate(inflater, container, false) binding.lifecycleOwner = viewLifecycleOwner return binding.root }
Solution
The recommended solution is to use fragment’s view lifecycle via getViewLifecycleOwner() or getViewLifecycleOwnerLiveData() which were added in Support Library 28.0.0 and AndroidX 1.0.0, so that LiveData will remove observers every time the fragment’s view is destroyed
Reference: 5 common mistakes when using Architecture Components
Answered By - Royi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.