Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getViewLifeCycleOwner() vs 'this' vs this.getActivity() in Androidx Fragments

We utilize LiveData's observe method quite a bit in our fragments. A recent release of the androidx fragment sdk is leading to Android Studio marking instances of liveDataObject.observe(this) as incorrect in favor of liveDataObject.observe(getViewLifecycleOwner()) .

Added a new Lint check that ensures you are using getViewLifecycleOwner() when observing LiveData from onCreateView(), onViewCreated(), or onActivityCreated(). (b/137122478) https://developer.android.com/jetpack/androidx/releases/fragment

We are worried about implementing this change because we do not understand how the functionality of getViewLifecycleOwner() compares to that of using this, and whether it will cause a conflict with the use of this or this.getActivity() when setting up the ViewModel in the fragment.

Additionally, we use the Android Navigation component and noticed that when the user navigates to different fragments within the same activity, each fragment's onDestroyView() method is called, but not onDestroy()

Here is an example of our code in onViewCreated()

 vm.getStemLengths().observe(this, stemLengths -> {
        this.stemLengths = new ArrayList<>(Stream.of(stemLengths).map(stemLength ->
                new SearchModel(Integer.toString(stemLength.getValue()))).toList());
  });

Later, in onDestroyView()

 vm.getStemLengths().removeObservers(this);

At the same time, depending on the fragment, the ViewModel that houses the LiveData is set up with one of the following:

 vm = new ViewModelProvider(this.getActivity()).get(PrepareVM.class);

To persist the viewmodel across fragments in the activity.

Or:

vm = new ViewModelProvider(this).get(AprobacionVM.class);

If the VM does not need to be persisted outside of the current fragment

So to summarize, will changing this to getViewLifeCycleOwner() when observing LiveData objects in fragments' onCreateView() conflict with the ViewModel patterns / Navigation component? Could there be an instance for example where the livedata change ends up triggering an observer from a previous fragment in the same activity that a user navigates away from?

From the documentation of getViewLifeCycleOwner it seems that making this change may allow us to remove the removeObservers() call in each fragment's onDestroyView(). Is that the correct understanding?

like image 328
user1114 Avatar asked Nov 18 '19 07:11

user1114


People also ask

How do I use getViewLifecycleOwner in activity?

Use getViewLifecycleOwner() as the LifecycleOwner instead of a Fragment instance when observing a LiveData object. Use getViewLifecycleOwner() as the LifecycleOwner instead of a Fragment instance when observing a LiveData object.

What is Androidx fragment?

fragment:fragment. Official Description: The Support Library is a static library that you can add to your Android application in order to use APIs that are either not available for older platform versions or utility APIs that aren't a part of the framework APIs. Compatible on devices running API 14 or later.

What are kotlin fragments?

A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment. The fragment's view hierarchy becomes part of, or attaches to, the host's view hierarchy.


1 Answers

Fragment implements LifecycleOwner which maps its create and destroy events to the fragment's onCreate and onDestroy, respectively.

Fragment.getViewLifecycleOwner() maps its create and destroy events to the fragment's onViewCreated and onDestroyView, respectively. The precise sequence is described here.

If you're working with views in your observers, you'll want the view lifecycle. Otherwise you might get updates when the view hierarchy is invalid, which may lead to crashes.

From the documentation of getViewLifeCycleOwner it seems that making this change may allow us to remove the removeObservers() call in each fragment's onDestroyView().

Correct.

like image 134
Eugen Pechanec Avatar answered Sep 22 '22 04:09

Eugen Pechanec