我如何知道Froyo何时进行配置更改? [英] How do I tell when a configuration change is happening in Froyo?

查看:81
本文介绍了我如何知道Froyo何时进行配置更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我希望播放媒体文件,并且如果用户旋转屏幕(破坏活动")继续播放,但是如果用户移动到另一个活动"或出现另一个活动",我希望它停止播放在此上方,无论如何,他们都按下后退按钮.

In my app, I want a media file to play, and to keep playing if the user rotates the screen (destroying the Activity), but I want it to stop playing if the user moves to a different Activity or another Activity appears over this one, they press the back button, whatever.

我相信Honeycomb中有一个用于此的API,但是我需要在Android 2.2中可以使用的东西.

I believe there's an API for this in Honeycomb, but I need something that will work in Android 2.2.

我宁愿不使用onConfigurationChanged自己处理所有配置更改-听起来很多工作和潜在的错误-并且onRetainNonConfigurationInstance()的问题是直到onStop触发后它才运行--但如果合适的话,onPause将是暂停媒体的合理位置.

I'd rather not use onConfigurationChanged to handle all the configuration changes myself--that sounds like a lot of work and potential bugs--and the issue with onRetainNonConfigurationInstance() is that it doesn't run until after onStop fires--but onPause would be the logical place to pause the media, if appropriate.

在onPause中,有没有办法判断Activity是否由于配置更改而暂停?是出于其他原因吗?

Is there a way, in onPause, to tell if the Activity is being paused for a configuration change versus another reason?

推荐答案

您的解决方案至少有一个潜在的问题.

Your solution has at least one potential problem.

根据Android文档:

According to Android documentation:

某些设备配置可以在运行时更改(例如屏幕方向,键盘可用性和语言).发生此类更改时,Android将重新启动正在运行的Activity(先调用onDestroy(),再调用onCreate()).

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()).

测试旋转变化仅能处理一种情况.由于这很可能是配置更改的最常见原因,所以这是一个好的解决方案.但是,如果您可以处理所有情况,无论是将来的Android版本中现有的还是添加的,而又无需自己处理配置的开销,那么该怎么办?

Testing for a change in rotation only handles one case. Since this is likely to be the most common cause of configuration change, it's an OK solution. But what if you could handle all cases, existing or added in some future version of Android, without the overhead of handling configuration yourself?

已更新为使用 onRetainNonConfigurationInstance .Android文档对此有话要说:

Updated to use onRetainNonConfigurationInstance. Android docs have this to say about it:

由系统调用,这是由于已知由于将立即为新配置创建新实例而导致的由于配置更改而破坏活动的一部分.

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()以在停止媒体后发生,但并不完全确定.猜猜这取决于停止介质的含义,以及销毁对停止介质可能产生的影响.

Also changed chain to super.onDestroy() to happen after stopping media, but not entirely sure about that. Guess it depends on what stopping media means, and what effect destroying may have on stopping media.

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();
}    

这篇关于我如何知道Froyo何时进行配置更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆