简体   繁体   English

在AndroidX中不推荐使用onRetainCustomNonConfigurationInstance

[英]onRetainCustomNonConfigurationInstance deprecated in AndroidX

onRetainCustomNonConfigurationInstance was deprecated in AndroidX when the first version got released back in 2018! 当第一个版本于2018年发布时, onRetainCustomNonConfigurationInstance中不推荐使用onRetainCustomNonConfigurationInstance! As the AndroidX Activity library says in the release notes: AndroidX活动库在发行说明中所述:

onRetainCustomNonConfigurationInstance has been deprecated. onRetainCustomNonConfigurationInstance已弃用。 Use a ViewModel for storing objects that need to survive configuration changes. 使用ViewModel来存储需要保留配置更改的对象。

I just want a single object to survive configuration changes, this was the purpose of onRetainCustomNonConfigurationInstance ! 我只希望单个对象能够幸免于配置更改,这就是onRetainCustomNonConfigurationInstance的目的!

For my use case, I want an instance of a Dagger graph to survive configuration changes in an Activity but using AAC ViewModel doesn't feel right to me for that use case. 对于我的用例,我希望Dagger图的实例能够在Activity中的配置更改中幸存下来,但在该用例中,使用AAC ViewModel对我来说不合适。 Dagger injects my ViewModels, I don't want to wrap my graph in another ViewModel just for the sake of making it survive configuration changes. Dagger注入了我的ViewModel,我不想仅仅为了使它在配置更改后生存下来而将我的图形包装在另一个ViewModel中。

Is there any other way I can make an object survive configuration changes? 还有什么其他方法可以使对象在配置更改后继续存在?

Using ViewModel is the most accurate and recommended way to make an object survive configuration changes, you should use it. 使用ViewModel是使对象不受配置更改影响的最准确和推荐的方法,应使用它。 You could've used onSaveInstanceState but that'd force all the objects needing to support Parcelable , that's not only reasonable, it's sometimes impossible. 您可以使用onSaveInstanceState但是这将迫使所有需要支持Parcelable的对象,这不仅合理,而且有时是不可能的。

To replace onRetainCustomNonConfigurationInstance , you can use the same APIs that support ViewModel in a way that handles all the complexity for you. 要替换onRetainCustomNonConfigurationInstance ,您可以使用支持ViewModel的相同API来处理您的所有复杂性。

You can use this implementation of LongLastingElement API (code here) that uses ViewModel under the hood and removes all the boilerplate code to make an object survive configuration changes. 您可以使用LongLastingElement API的此实现(此处为代码) ,该实现在后台使用ViewModel并删除所有样板代码,以使对象在配置更改后仍然有效。

For your example, to make the Dagger graph survive configuration changes with this API, the code would look like this: 在您的示例中,为了使Dagger图在使用此API进行配置更改后仍然有效,代码如下所示:

class LoginActivity : AppCompatActivity() {

    private lateinit var loginComponent: LoginComponent

    ...

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        loginComponent = LongLastingElement.getInstance<LoginComponent>(this).getElement {
            (applicationContext as MyDaggerApplication).appComponent.loginComponent().create()
        }

        loginComponent.inject(this)
    }
}

LongLastingElement has a static method called getInstance where you specify the type of the object that needs to be stored and you pass a lifecycle owner in. Then, the getElement method is called, it receives a lambda as a parameter that needs to create an instance of the object you want to store. LongLastingElement有一个称为getInstance的静态方法,您可以在其中指定需要存储的对象的类型,并在其中传递生命周期所有者。然后,调用getElement方法,该方法将接收lambda作为需要创建实例的参数。您要存储的对象。

Since the Lifecycle owner is used to obtain the LongLastingElement instance, the lambda that getElement takes as a parameter will be only called once. 由于生命周期所有者用于获取LongLastingElement实例,因此将getElement用作参数的lambda仅被调用一次。 It'll create an instance of the object the very first time is called and the same instance will be reused after configuration changes and subsequent calls to getElement . 它将在第一次调用时创建该对象的一个​​实例,并且在更改配置和随后对getElement后续调用之后,该实例将被重用。 This works for any lifecycle owner such as Activities and Fragments. 这适用于任何生命周期所有者,例如“活动”和“片段”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM