手动清除 Android ViewModel? [英] Manually clearing an Android ViewModel?

查看:40
本文介绍了手动清除 Android ViewModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题有点过时了,因为 Google 已经让我们能够将 ViewModel 范围限定为导航图.更好的方法(而不是试图清除活动范围模型)是为适当数量的屏幕创建特定的导航图,并为这些屏幕创建范围.

This question is a bit out of date now that Google has given us the ability to scope ViewModel to navigation graphs. The better approach (rather than trying to clear activity-scoped models) would be to create specific navigation graphs for the right amount of screens, and scope to those.

参考 android.arch.lifecycle.ViewModel 类.

ViewModel 的范围限定于它相关的 UI 组件的生命周期,因此在基于 Fragment 的应用程序中,这将是片段生命周期.这是一件好事.

ViewModel is scoped to the lifecycle of the UI component it relates to, so in a Fragment-based app, that will be the fragment lifecycle. This is a good thing.

在某些情况下,人们希望在多个片段之间共享一个 ViewModel 实例.具体来说,我对许多屏幕与相同的底层数据相关的情况感兴趣.

In some cases one wants to share a ViewModel instance between multiple fragments. Specifically I am interested in the case where many screens relate to the same underlying data.

(当多个相关片段显示在同一屏幕上时,文档提出了类似的方法,但 这可以通过使用单个主机来解决根据下面的答案片段.)

(The docs suggest similar approach when multiple related fragments are displayed on the same screen but this can be worked around by using a single host fragment as per answer below.)

这在 官方 ViewModel 文档 中进行了讨论:

This is discussed in the official ViewModel documentation:

ViewModels 也可以作为不同层之间的通信层活动的片段.每个Fragment都可以获取ViewModel通过他们的活动使用相同的密钥.这允许通信以解耦的方式在片段之间,这样它们就永远不需要直接与另一个 Fragment 对话.

ViewModels can also be used as a communication layer between different Fragments of an Activity. Each Fragment can acquire the ViewModel using the same key via their Activity. This allows communication between Fragments in a de-coupled fashion such that they never need to talk to the other Fragment directly.

换句话说,为了在代表不同屏幕的片段之间共享信息,ViewModel 应该被限定在 Activity 生命周期内(根据 Android 文档,这也可以是在其他共享实例中使用).

In other words, to share information between fragments that represent different screens, the ViewModel should be scoped to the Activity lifecycle (and according to Android docs this can also be used in other shared instances).

现在在新的 Jetpack 导航模式中,建议使用一个活动/多个片段"架构.这意味着活动在应用程序被使用的整个时间内都存在.

Now in the new Jetpack Navigation pattern, it is recommended to use a "One Activity / Many Fragments" architecture. This means that the activity lives for the whole time the app is being used.

即任何在 Activity 生命周期范围内的共享 ViewModel 实例将永远不会被清除 - 内存保持在不断使用中.

i.e. any shared ViewModel instances that are scoped to Activity lifecycle will never be cleared - the memory remains in constant use.

为了保留内存并在任何时间点使用尽可能少的内存,如果能够在不再需要时清除共享的 ViewModel 实例会很好.

With a view to preserving memory and using as little as required at any point in time, it would be nice to be able to clear shared ViewModel instances when no longer required.

如何手动清除 ViewModel 中的 ViewModelStore 或持有者片段?

How can one manually clear a ViewModel from it's ViewModelStore or holder fragment?

推荐答案

如果你检查代码 这里 你会发现,你可以获得ViewModelStore 来自 ViewModelStoreOwnerFragment,例如,FragmentActivity 实现了那个接口.

If you check the code here you'll find out, that you can get the ViewModelStore from a ViewModelStoreOwner and Fragment, FragmentActivity for example implements, that interface.

从那里你可以只调用 viewModelStore.clear(),正如文档所说:

Soo from there you could just call viewModelStore.clear(), which as the documentation says:

 /**
 *  Clears internal storage and notifies ViewModels that they are no longer used.
 */
public final void clear() {
    for (ViewModel vm : mMap.values()) {
        vm.clear();
    }
    mMap.clear();
}

注意:这将清除特定 LifeCycleOwner 的所有可用 ViewModel,这不允许您清除特定的 ViewModel.

N.B.: This will clear all the available ViewModels for the specific LifeCycleOwner, this does not allow you to clear one specific ViewModel.

这篇关于手动清除 Android ViewModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆