在哪里可以使用 onSaveInstanceState 参数传递不同的Activity?

Posted

技术标签:

【中文标题】在哪里可以使用 onSaveInstanceState 参数传递不同的Activity?【英文标题】:Where can be use onSaveInstanceState where parameters are passed different Activity? 【发布时间】:2016-09-22 13:40:30 【问题描述】:

对于 Acitivity,已经有一个名为 onSaveInstacestate(Bundle) 的方法用于存储被覆盖的方法的活动数据。

如我所见,有两种不同的 onSaveInstanceState 参数传递方式不同,如下所示。

@Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState)
    
        super.onSaveInstanceState(outState, outPersistentState);
        Log.i("test", "onSaveInstanceState called **********");
    

@Override
    protected void onSaveInstanceState(Bundle outState)
    
         super.onSaveInstanceState(outState);
          Log.i("test", "onSaveInstanceState with bundle only called");
    

那么,这两种方法在什么情况下可以使用呢? 请详细描述。 提前致谢。

【问题讨论】:

【参考方案1】:

这值得一个扩展的答案。正如接受的答案所述,由于 API 级别 21,onSaveInstanteState 存在额外的重载。

从 API 级别 1 (Docs) 开始可用:

void onSaveInstanceState (Bundle outState)

API 级别 21 (Docs) 引入的新增功能:

void onSaveInstanceState (Bundle outState, PersistableBundle outPersistentState)

带有PersistableBundle 的后一个不是前一个的替代品。它仅在Activity 属性R.attr.persistableMode 设置为persistAcrossReboots 时使用。当这样的Activity 将被持久化时,onSaveInstanceState (Bundle outState, PersistableBundle outPersistentState) 将被调用并且您会收到一个PersistableBundle 来存储您的实例状态。

要恢复将R.attr.persistableMode 设置为persistAcrossRebootsActivity 的状态,有

void onRestoreInstanceState (Bundle savedInstanceState, PersistableBundle persistentState)

请注意,如果调用带有PersistableBundle 的那个,则不会调用onRestoreInstanceState (Bundle savedInstanceState)。我认为onSaveInstanceState 也是如此,但我没有检查过它,并且在 API 级别 28 时文档也没有提到它。

onCreate() 也有一个适当的重载。

【讨论】:

这是有道理的。接受的答案具有误导性,您不能盲目地在更高的api中使用新的。【参考方案2】:

从 API 级别 21 开始,onSaveInstanceState() 有一个名为的新参数,它采用 PersistableBundle 的对象。您可以在Docs 上阅读有关 PersistableBundle 的更多信息

总之,

适用于 API 21 及更高版本

@Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState)
    
        super.onSaveInstanceState(outState, outPersistentState);
        Log.i("test", "onSaveInstanceState called **********");
    

对于小于 20 的 API

@Override
    protected void onSaveInstanceState(Bundle outState)
    
         super.onSaveInstanceState(outState);
          Log.i("test", "onSaveInstanceState with bundle only called");
    

【讨论】:

感谢 Onkar。我阅读了文档.. 但需要看看如何在 onSaveInstancestate 和 onRestoreInstace 状态下使用这个 PersistableBundle ? ...为了版本之间的兼容性?【参考方案3】:

除了所说的,我不能说更多,但这是一些帮助代码可以帮助你 我在基本活动中执行此操作以确保始终保存我的数据

public abstract void saveInstanceState(Bundle outState, PersistableBundle outPersistentState);

@Override
public final void onSaveInstanceState(@NonNull Bundle outState, PersistableBundle outPersistentState) 
    saveInstanceState(outState, outPersistentState);
    super.onSaveInstanceState(outState, outPersistentState);


@Override
protected final void onSaveInstanceState(@NonNull Bundle outState) 
    saveInstanceState(outState, null);
    super.onSaveInstanceState(outState);

我正在使用 final 关键字,所以我不会忘记并覆盖其中一种方法 而不是我的自定义摘要

【讨论】:

以上是关于在哪里可以使用 onSaveInstanceState 参数传递不同的Activity?的主要内容,如果未能解决你的问题,请参考以下文章

Android开发之InstanceState详解

我们可以在哪里使用列表初始化?

为啥 GetHashCode 很重要,我们可以在哪里使用它? [复制]

SQL - 是不是可以在哪里使用别名? [复制]

什么是网页的HTML,在哪里可以看到?

为啥 MDX 中可能有超过 2 个轴,我可以在哪里使用它?