Androidx 片段中的 getViewLifeCycleOwner() 与 'this' 与 this.getActivity()
getViewLifeCycleOwner() vs 'this' vs this.getActivity() in Androidx Fragments
我们在片段中大量使用了 LiveData 的 observe
方法。最近发布的 androidx 片段 sdk 导致 Android Studio 将 liveDataObject.observe(this)
的实例标记为不正确,支持 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
我们担心实施此更改,因为我们不了解 getViewLifecycleOwner()
的功能与使用 this
的功能相比如何,以及它是否会与使用 [=] 产生冲突20=] 或 this.getActivity()
在片段中设置 ViewModel 时。
此外,我们使用 Android 导航组件并注意到当用户导航到同一 activity 中的不同片段时,每个片段的 onDestroyView()
方法都会被调用,但不是 onDestroy()
这是我们在 onViewCreated()
中的代码示例
vm.getStemLengths().observe(this, stemLengths -> {
this.stemLengths = new ArrayList<>(Stream.of(stemLengths).map(stemLength ->
new SearchModel(Integer.toString(stemLength.getValue()))).toList());
});
稍后,在onDestroyView()
vm.getStemLengths().removeObservers(this);
同时,根据片段,包含 LiveData 的 ViewModel 设置为以下之一:
vm = new ViewModelProvider(this.getActivity()).get(PrepareVM.class);
在 activity 中跨片段持久保存视图模型。
或:
vm = new ViewModelProvider(this).get(AprobacionVM.class);
如果VM不需要在当前分片外持久化
总而言之,在观察片段 onCreateView()
中的 LiveData 对象时,将 this
更改为 getViewLifeCycleOwner()
是否会与 ViewModel 模式/导航组件冲突?例如,是否存在一个实例,其中实时数据更改最终会触发用户导航离开的同一 activity 中先前片段的观察者?
从 getViewLifeCycleOwner 的文档来看,进行此更改似乎可以让我们删除每个片段 onDestroyView()
中的 removeObservers()
调用。这样理解对吗?
Fragment
实现 LifecycleOwner
,它将其创建和销毁事件分别映射到片段的 onCreate
和 onDestroy
。
Fragment.getViewLifecycleOwner()
将其创建和销毁事件分别映射到片段的 onViewCreated
和 onDestroyView
。精确的顺序描述为 here.
如果您在观察者中使用视图,您将需要视图生命周期。否则,您可能会在视图层次结构无效时获得更新,这可能会导致崩溃。
From the documentation of getViewLifeCycleOwner it seems that making this change may allow us to remove the removeObservers() call in each fragment's onDestroyView().
正确。
它已经有一个错误。如果您对已经使用的片段执行新的 Intent,它会给出 getView is null 错误。为什么? activity是新建的
我们在片段中大量使用了 LiveData 的 observe
方法。最近发布的 androidx 片段 sdk 导致 Android Studio 将 liveDataObject.observe(this)
的实例标记为不正确,支持 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
我们担心实施此更改,因为我们不了解 getViewLifecycleOwner()
的功能与使用 this
的功能相比如何,以及它是否会与使用 [=] 产生冲突20=] 或 this.getActivity()
在片段中设置 ViewModel 时。
此外,我们使用 Android 导航组件并注意到当用户导航到同一 activity 中的不同片段时,每个片段的 onDestroyView()
方法都会被调用,但不是 onDestroy()
这是我们在 onViewCreated()
vm.getStemLengths().observe(this, stemLengths -> {
this.stemLengths = new ArrayList<>(Stream.of(stemLengths).map(stemLength ->
new SearchModel(Integer.toString(stemLength.getValue()))).toList());
});
稍后,在onDestroyView()
vm.getStemLengths().removeObservers(this);
同时,根据片段,包含 LiveData 的 ViewModel 设置为以下之一:
vm = new ViewModelProvider(this.getActivity()).get(PrepareVM.class);
在 activity 中跨片段持久保存视图模型。
或:
vm = new ViewModelProvider(this).get(AprobacionVM.class);
如果VM不需要在当前分片外持久化
总而言之,在观察片段 onCreateView()
中的 LiveData 对象时,将 this
更改为 getViewLifeCycleOwner()
是否会与 ViewModel 模式/导航组件冲突?例如,是否存在一个实例,其中实时数据更改最终会触发用户导航离开的同一 activity 中先前片段的观察者?
从 getViewLifeCycleOwner 的文档来看,进行此更改似乎可以让我们删除每个片段 onDestroyView()
中的 removeObservers()
调用。这样理解对吗?
Fragment
实现 LifecycleOwner
,它将其创建和销毁事件分别映射到片段的 onCreate
和 onDestroy
。
Fragment.getViewLifecycleOwner()
将其创建和销毁事件分别映射到片段的 onViewCreated
和 onDestroyView
。精确的顺序描述为 here.
如果您在观察者中使用视图,您将需要视图生命周期。否则,您可能会在视图层次结构无效时获得更新,这可能会导致崩溃。
From the documentation of getViewLifeCycleOwner it seems that making this change may allow us to remove the removeObservers() call in each fragment's onDestroyView().
正确。
它已经有一个错误。如果您对已经使用的片段执行新的 Intent,它会给出 getView is null 错误。为什么? activity是新建的