gpt4 book ai didi

android - 如何判断 Froyo 何时发生配置更改?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:58:25 26 4
gpt4 key购买 nike

在我的应用程序中,我希望播放一个媒体文件,并在用户旋转屏幕(破坏 Activity)时继续播放,但如果用户移动到另一个 Activity 或出现另一个 Activity,我希望它停止播放在这个上面,他们按了后退按钮,无论如何。

我相信 Honeycomb 中有一个 API,但我需要一些可以在 Android 2.2 中运行的东西。

我不想自己使用 onConfigurationChanged 来处理所有配置更改——这听起来像是很多工作和潜在的错误——而 onRetainNonConfigurationInstance() 的问题是它在 onStop 触发后才会运行—— - 但如果合适的话,onPause 将是暂停媒体的合乎逻辑的地方。

有没有办法在 onPause 中判断 Activity 是因为配置更改还是其他原因而暂停?

最佳答案

您的解决方案至少存在一个潜在问题。

根据 Android 文档:

Some device configurations can change during runtime (such as screen orientation, keyboard availability, and language). When such a change occurs, Android restarts the running Activity (onDestroy() is called, followed by onCreate()).

测试旋转变化只处理一种情况。由于这可能是配置更改的最常见原因,因此这是一个不错的解决方案。但是,如果您可以处理所有情况,无论是现有的还是 future 某个版本的 Android 中添加的,而无需自己处理配置的开销,情况会怎样呢?

已更新为使用 onRetainNonConfigurationInstance。 Android 文档是这样说的:

Called by the system, as part of destroying an activity due to a configuration change, when it is known that a new instance will immediately be created for the new configuration.

也将链更改为 super.onDestroy() 以在停止媒体后发生,但对此并不完全确定。猜猜这取决于停止媒体意味着什么,以及破坏可能对停止媒体产生什么影响。

private Boolean mConfigurationChange = false;

public Object onRetainNonConfigurationInstance() {
mConfigurationChange = true;
return null;
}

public void onDestroy() {
if (!mConfigurationChange) {
// Code to stop media file goes here.
}
super.onDestroy();
}

关于android - 如何判断 Froyo 何时发生配置更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8314935/

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