当其主机活动不在活动堆栈顶部并且设备已旋转时,将重新创建Android ViewModel [英] Android ViewModel recreated when its Host Activity was not in the top of Activity Stack and the device was rotated

查看:96
本文介绍了当其主机活动不在活动堆栈顶部并且设备已旋转时,将重新创建Android ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处于以下情况:

我有一个包含ViewModelOnboardActivity,我可以旋转OnboardActivity很多次,并且ViewModel可以在配置更改中持续存在而没有问题.

I have an OnboardActivity which contains a ViewModel, I can rotate this OnboardActivity many times and the ViewModel persist across configuration changes without issues.

但是,如果我用startActivityForResult(...)在此(OnboardActivity)上启动另一个Activity(FirebaseAuthActivity),然后在FirebaseAuthActivity中旋转设备并按返回按钮.当OnboardActivity到达堆栈顶部时,它将再次重新创建ViewModel实例.

However, if I launch another Activity(FirebaseAuthActivity) on top of this one (OnboardActivity) with startActivityForResult(...), and then in FirebaseAuthActivity I rotate the device and press the back button. When the OnboardActivity is brought to the top of the stack it recreates the ViewModel instance again.

这是ViewModel在体系结构组件中的正常行为吗?

Is this the normal behavior of ViewModel in architecture components?

有一种方法可以告诉OnboardActivity从屏幕弹出的方向不同于保存方向的堆栈中弹出时还是不结束吗?

Is there a way I can tell the OnboardActivity to not finish when it is pop from the stack with a screen orientation different than the one it was saved?

推荐答案

我已经在此处回答了类似的问题,到目前为止,这可能会帮助您修复自我.

I have answered to a similar question here, This might help you to fix your self as of now.

这是来自Android框架的错误. 错误详细信息

This was a bug from an android framework. Bug details

此修复程序在28.0.0-alpha3和AndroidX 1.0.0-alpha3中可用

The fix is available in 28.0.0-alpha3 and AndroidX 1.0.0-alpha3

但是,如果您自己现在不想更新到上述版本,则可以这样解决(我知道这是一个糟糕的解决方案,但我没有看到其他好的方法)

But if you don't want to update to above versions now itself, Then you can solve like this (I know this is a bad solution but I didn't see any other good way)

在您的活动中,重写onDestroy方法,然后在调用super.onDestroy之前将所有必填字段保存到局部变量中.现在调用super.onDestroy,然后再次初始化ViewModel,并将必填字段分配回ViewModel的新实例

In your activity override onDestroy method and save all the required fields to local variables before calling super.onDestroy. Now call super.onDestroy then Initialize your ViewModel again and assign the required fields back to your new instance of ViewModel

关于 isFinishing

下面的代码在Kotlin中:

Below code is in Kotlin:

override fun onDestroy() {

  if (!isFinishing) { //isFinishing will be false in case of orientation change
      val oldViewModel = obtainViewModel()

      val requiredFieldValue = oldViewModel.getRequiredFieldValue()

      super.onDestroy

     val newViewModel = obtainViewModel()

     if (newViewModel != oldViewModel) { //View Model has been destroyed
          newViewModel.setRequiredFieldValue(requiredFieldValue)
      }
  } else {
     super.onDestroy
  }
 }

private fun obtainViewModel(): SampleViewModel {
      return ViewModelProviders.of(this).get(SampleViewModel::class.java)
}

这篇关于当其主机活动不在活动堆栈顶部并且设备已旋转时,将重新创建Android ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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