我如何知道Froyo何时进行配置更改?

编程入门 行业动态 更新时间:2024-10-11 17:22:47
本文介绍了我如何知道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何时进行配置更改?

本文发布于:2023-11-13 11:06:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584193.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Froyo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!