- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
当使用后退或概览按钮将当前任务(我的应用程序)置于后台时,我需要一种方法来恢复以前的 Android 任务。例如,当收到 SIP 调用时,youtube 应用程序会播放一段视频。软电话任务被带到前台,然后调用被接听。通话结束后,按返回或概览按钮将显示 youtube 应用程序并继续播放视频。此类 Android 应用程序的一个示例是 Linphone。我想知道如何以编程方式实现这一目标。
最佳答案
正如其他人所述和here ,Android 会自动为您处理。但是,如果您需要在进入/进入/离开后台状态时显式添加任何内容,那么您还可以覆盖 onSaveInstanceState()
和 onRestoreInstanceState()
方法,它们将被相应地调用.
As your activity begins to stop, the system calls the
onSaveInstanceState()
method so your activity can save state information to an instance state bundle. The default implementation of this method saves transient information about the state of the activity's view hierarchy, such as the text in anEditText
widget or the scroll position of aListView
widget.To save additional instance state information for your activity, you must override
onSaveInstanceState()
and add key-value pairs to theBundle
object that is saved in the event that your activity is destroyed unexpectedly. If you overrideonSaveInstanceState()
, you must call the superclass implementation if you want the default implementation to save the state of the view hierarchy.
@Override
protected void onRestoreInstanceState(Bundle outState) {
if (outState != null) {
Crashlytics.log(1, "FormActivity", "Method:onRestoreInstanceState, Msg: saved instance is not null");
if (outState.containsKey("record")
&& Session.getCurrentRecord() == null) {
Session.setCurrentRecord(
gson.fromJson(
outState.getString("record"),
Record.class
)
);
}
if (outState.containsKey("user")
&& Session.getCurrentUser() == null) {
Session.setCurrentUser(
gson.fromJson(
outState.getString("user"),
User.class
)
);
}
}
super.onRestoreInstanceState(outState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
Session.setCurrentRecord(record);
outState.putString("record", gson.toJson(Session.getCurrentRecord()));
outState.putString("user", gson.toJson(Session.getCurrentUser()));
super.onSaveInstanceState(outState);
}
关于java - 我的应用程序在 Android 上置于后台后如何恢复以前的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52919445/
我是 firebase 的新手,我正在尝试分页查询。我喜欢有一个“下一个”和“上一个”按钮。我的下一个按钮工作正常,我的问题是单击上一个 引用:https://firebase.google.com/
抱歉,标题这么蹩脚,但我只是不知道该放什么,希望你能理解。另外,我不知道以前是否有人问过类似的问题,因为我不知道合适的关键字 - 因此也无法用谷歌搜索。 基本上...在查看preg_match_all
我想在 TFS 中 check out 一个检入文件的先前版本。我可以轻松获得特定文件的变更集 ID 列表,但无法弄清楚如何 checkout 以前的版本。 我目前的代码: var workspace
我想使用 @FunctionalInterface来 self 代码中的 Java 8,但我希望能够将生成的类文件与 Java 6 一起使用。我认为我应该将源版本设为 1.8 , 目标版本为 1.6
自从 versions 被删除以来,我一直无法找到安装以前版本软件的方法。命令并点击 Homebrew。我在 2008 Mac Pro (3,1) 上运行 macOS 10.14.3 (Mojave)
当我开始当前的项目时,App Store 中已经有一个应用程序。此应用程序仅适用于 iPhone。 我的第一个任务是测试和构建一个也可以在 iPod Touch 上运行的版本。 大约 3 周前,App
我在 GitHub 上有一个曾经是 fork 的 repo,但现在不是了,因为我已经删除了原始项目的任何痕迹并开始了一个同名的新项目。 但是,GitHub 仍然表示该项目是 fork 的。有什么方法可
我是一名优秀的程序员,十分优秀!