幸存的配置更改机制如何工作?
how does surviving configuration changes mechanisms work?
我有三个关于生存配置更改的类似问题。我知道三种在配置更改时保留对象的方法,但我不知道它们是如何工作的。
覆盖 onSaveInstanceState
。我知道它将数据放入包中。但它如何在配置更改后存活下来?真正的束是什么?它属于 activity 吗?应用程序中只有一个捆绑包吗? (我知道我们可以创建不同的实例,但是当我们用 putExtra
或 onSaveInstanceState
或任何其他方法放置数据时,它们会去同一个地方吗?)
使用 onRetainNonConfigurationInstance()
和 getLastNonConfigurationInstance()
。这用于在配置更改中保留 viewModelStore
。那么它是如何工作的,它把数据放在哪里?(我知道 deprication)
Retained(Holder) 片段和片段中的 setRatinInstance()
方法。同样的问题(我认为 activity 只是为此使用了 onRetainNonConfigurationInstance()
,如果我错了请纠正我)
我为此在网络和 Whosebug 上进行了搜索,但没有找到任何内容。我也尝试过实际的代码,但没有成功!
因为这些问题是相关的,所以我想我把它们全部放在一个问题中。希望一切都好
Overriding onSaveInstanceState. I know it puts the data in the bundle. but how does it survive configuration changes? what really bundle is? does it belong to activity? do we have only one bundle in the app? (I know we can make different instances but when we put our data with putExtra or onSaveInstanceState or any other method, are they going to the same place?)
这并不是真正的“跨配置更改的保留”,那是 more-so“节省 process-level 持久性”,然后 re-used 用于配置更改。我不认为它是“保留”的原因是因为对象被打包为 Parcelable,然后在恢复时创建一个副本。
反正the ActivityRecord has a bundle,每个任务栈都有自己的ActivityRecords,任务栈在进程死亡时由系统保存和恢复。这种机制恰好“也处理配置更改”。
Intent.putExtra
非常相似,是的。
Using onRetainNonConfigurationInstance() and getLastNonConfigurationInstance(). this is used to retain viewModelStore across configuration changes. so how it works and where does it put the data?(I know about deprication)
与 onSaveInstanceState
不同,这实际上是一个 OS-level 回调,仅用于跨配置更改保留对象。曾经有 onRetainCustomNonConfigurationInstance
/getLastCustomNonConfigurationInstance
,但它已被弃用,取而代之的是 ViewModel,因为 ViewModel 旨在成为跨配置更改保留的“唯一标准”方式。不过,在内部,它只是 viewModelStore
(这是一张地图)的 onRetainNonConfigurationInstance
。
Retained(Holder) Fragment and setRetainInstance() method in the fragment. the same question(I think the activity just uses the onRetainNonConfigurationInstance() for this, correct me if I'm wrong)
是的,但不幸的是,在最新的 Jetpack Fragments 版本中也弃用了(为了简化片段的交互及其生命周期)。片段管理器保留为non-config,保留的片段也随之保留,使用onRetainNonConfigurationInstance()
的FragmentActivity
(我想现在叫FragmentManagerViewModel
)。
Non-configuration 个实例保存在 the ActivityClientRecord which are not persisted by the ActivityStack.
希望涵盖的内容足够了。
我有三个关于生存配置更改的类似问题。我知道三种在配置更改时保留对象的方法,但我不知道它们是如何工作的。
覆盖
onSaveInstanceState
。我知道它将数据放入包中。但它如何在配置更改后存活下来?真正的束是什么?它属于 activity 吗?应用程序中只有一个捆绑包吗? (我知道我们可以创建不同的实例,但是当我们用putExtra
或onSaveInstanceState
或任何其他方法放置数据时,它们会去同一个地方吗?)使用
onRetainNonConfigurationInstance()
和getLastNonConfigurationInstance()
。这用于在配置更改中保留viewModelStore
。那么它是如何工作的,它把数据放在哪里?(我知道 deprication)Retained(Holder) 片段和片段中的
setRatinInstance()
方法。同样的问题(我认为 activity 只是为此使用了onRetainNonConfigurationInstance()
,如果我错了请纠正我)
我为此在网络和 Whosebug 上进行了搜索,但没有找到任何内容。我也尝试过实际的代码,但没有成功!
因为这些问题是相关的,所以我想我把它们全部放在一个问题中。希望一切都好
Overriding onSaveInstanceState. I know it puts the data in the bundle. but how does it survive configuration changes? what really bundle is? does it belong to activity? do we have only one bundle in the app? (I know we can make different instances but when we put our data with putExtra or onSaveInstanceState or any other method, are they going to the same place?)
这并不是真正的“跨配置更改的保留”,那是 more-so“节省 process-level 持久性”,然后 re-used 用于配置更改。我不认为它是“保留”的原因是因为对象被打包为 Parcelable,然后在恢复时创建一个副本。
反正the ActivityRecord has a bundle,每个任务栈都有自己的ActivityRecords,任务栈在进程死亡时由系统保存和恢复。这种机制恰好“也处理配置更改”。
Intent.putExtra
非常相似,是的。
Using onRetainNonConfigurationInstance() and getLastNonConfigurationInstance(). this is used to retain viewModelStore across configuration changes. so how it works and where does it put the data?(I know about deprication)
与 onSaveInstanceState
不同,这实际上是一个 OS-level 回调,仅用于跨配置更改保留对象。曾经有 onRetainCustomNonConfigurationInstance
/getLastCustomNonConfigurationInstance
,但它已被弃用,取而代之的是 ViewModel,因为 ViewModel 旨在成为跨配置更改保留的“唯一标准”方式。不过,在内部,它只是 viewModelStore
(这是一张地图)的 onRetainNonConfigurationInstance
。
Retained(Holder) Fragment and setRetainInstance() method in the fragment. the same question(I think the activity just uses the onRetainNonConfigurationInstance() for this, correct me if I'm wrong)
是的,但不幸的是,在最新的 Jetpack Fragments 版本中也弃用了(为了简化片段的交互及其生命周期)。片段管理器保留为non-config,保留的片段也随之保留,使用onRetainNonConfigurationInstance()
的FragmentActivity
(我想现在叫FragmentManagerViewModel
)。
Non-configuration 个实例保存在 the ActivityClientRecord which are not persisted by the ActivityStack.
希望涵盖的内容足够了。