gpt4 book ai didi

java - 我的应用程序在 Android 上置于后台后如何恢复以前的应用程序

转载 作者:搜寻专家 更新时间:2023-11-01 09:23:09 25 4
gpt4 key购买 nike

当使用后退或概览按钮将当前任务(我的应用程序)置于后台时,我需要一种方法来恢复以前的 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 an EditText widget or the scroll position of a ListView widget.

To save additional instance state information for your activity, you must override onSaveInstanceState() and add key-value pairs to the Bundle object that is saved in the event that your activity is destroyed unexpectedly. If you override onSaveInstanceState(), 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);
}

Source 1 Source 2

关于java - 我的应用程序在 Android 上置于后台后如何恢复以前的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52919445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com