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

查看:694
本文介绍了手动清除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文档中进行了讨论:

ViewModels也可以用作不同层之间的通信层 活动的片段.每个片段都可以获取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.

如何从ViewModelStore或持有人片段中手动清除ViewModel?

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

推荐答案

如果您检查代码

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();
}

N.B.::这将清除特定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天全站免登陆