Android Activities中onCreate方法二次实现有什么用?

What is the usage of onCreate method second implementation in Android Activities?

我一直在我的 Activity 生命周期中使用 onCreate 方法来启动或从保存的状态恢复,但最近发现还有另一个包含 PersistableBundle 的 onCreate 方法:

@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);

}

才发现是从Android21.

开始加的

谁能提供有关此方法的完整信息,调用时间和用法吗?

根据我收集到的信息,如果您在清单中的 activity 上设置 属性,如下所示:

<activity 
   android:name=".MainActivity"
   android:persistableMode="persistAcrossReboots"
</activity>

然后您可以使用PersistableBundle在系统关闭并重新启动后恢复数据。换句话说,只要应用程序处于活动状态,普通的 Bundle 对象就会保留您的 savedInstanceState 记录。您可以使用 PersistableBundle 在系统关闭时保存数据。

您也可以使用 persistNeverpersistRootOnly 代替 persistAcrossReboots

您可以在此处找到有关文档的更多信息:https://developer.android.com/reference/android/R.attr.html#persistableMode