什么时候调用 onCleared 视图模型 [英] When is the viewmodel onCleared called

查看:34
本文介绍了什么时候调用 onCleared 视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ViewModel 是独立于活动/片段生命周期还是仅独立于它们的配置更改.它们什么时候会不复存在并调用后续的 onCleared() 方法.viewModel 可以与另一个 Activity 共享吗?

情况:

Activity1+viewModel1--->(旋转)--->Activity1+viewModel1--->(启动意图)---> Activity2+viewModel1

这种共享是否可行?这是一种很好的做法.

此外,由于应用程序生命周期回调,两者的 onPause->onStop->onDestroy 是相同的

1.activity旋转和

2.当一个Activity结束时,

ViewModel 如何在内部确定调用 onCleared 并最终结束其生命周期的正确时间.

<小时>

调查结果:

ViewModel 在内部使用 holderFragment 来保存 Activity 的实例,并使用类似片段的 setRetainInstance 方法来说明配置更改.

解决方案

ViewModel 是独立于 Activity/Fragment 生命周期还是仅仅依赖于他们的配置更改.

ViewModel (VM) 独立于配置更改,并在 Activity/Fragment 被销毁时被清除.

以下是来自官方网站的

<块引用>

viewModel 可以与另一个 Activity 共享吗?

您不应该对活动这样做.然而片段可以共享一个 ViewModel 使用它们的活动范围来处理它们之间的通信

<块引用>

ViewModel 如何在内部确定调用 onCleared 并最终结束其生命周期的正确时间?

当应用程序进入后台并终止应用程序进程以释放系统内存时,将调用 VM 的 onCleared.

请参阅是否让 ViewModels 持久化我的数据? 部分来自此 Android 开发人员的帖子 ViewModels:持久化、onSaveInstanceState()、恢复 UI 状态和加载器

如果您希望用户能够将应用程序置于后台,然后在三个小时后返回到完全相同的状态,您还应该保留数据.这是因为一旦您的 Activity 进入后台,如果设备内存不足,您的应用进程就会停止.

如果应用进程和活动停止,那么 ViewModel 也将被清除.

Are ViewModels independent of activity/fragment lifecycles or just their configuration changes. When will they cease to exist and the subsequent onCleared() method called. Can the viewModel be shared with another Activity ?

A situation:

Activity1+viewModel1--->(rotation)--->Activity1+viewModel1
--->(launch Intent)--->Activity2+viewModel1

is this sharing possible and is it a good practice.

Also, since the app lifecycle callbacks, onPause->onStop->onDestroy is same for both

1.activity rotating and

2.when an Activity ends,

how is a ViewModel figuring out internally the right time to call onCleared and finally end its lifecycle.


Findings:

the ViewModel uses a holderFragment internally to hold an instance of the activity and uses the setRetainInstance method like fragments to account for configuration changes.

Source: dive-inside-of-androids-viewmodel-architecture-components

解决方案

Are ViewModels independent of activity/fragment lifecycles or just their configuration changes.

ViewModels (VMs) are independent of configuration changes and are cleared when activity/fragment is destroyed.

Following is the lifecycle of ViewModel from official site:

Can the viewModel be shared with another Activity ?

You shouldn't do that with Activities. However fragments can share a ViewModel using their activity scope to handle communication between them

How is a ViewModel figuring out internally the right time to call onCleared and finally end its lifecycle?

A VM's onCleared is called when the app is put into the background and the app process is killed in order to free up the system's memory.

See the Do ViewModels persist my data? section from this Android Developer's post, ViewModels: Persistence, onSaveInstanceState(), Restoring UI State and Loaders

If you want the user to be able to put the app into the background and then come back three hours later to the exact same state, you should also persist data. This is because as soon as your activity goes into the background, your app process can be stopped if the device is running low on memory.

If the app process and activity are stopped, then the ViewModel will be cleared as well.

这篇关于什么时候调用 onCleared 视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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