user1114
user1114

Reputation: 1169

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?

Upvotes: 23

Views: 12666

Answers (2)

Herman Van Der Blom
Herman Van Der Blom

Reputation: 812

There is already a bug with it. If you do a new Intent for a fragment already use it gives getView is null error. Why? The activity is newly creared.

Upvotes: 0

Eugen Pechanec
Eugen Pechanec

Reputation: 38243

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.

Upvotes: 21

Related Questions