Android - 暂停 Activity 后在 onResume() 之前调用 onConfigurationChanged()

标签 android

在我的 Activity 中,我在播放视频时重写了 onConfigurationChanged() 方法,我不想等待一秒钟,直到系统再次准备好在我更改设备方向后继续播放视频。这工作得很好 - 我的视频继续流畅地播放并且方向发生变化,同时视频被重新缩放以适合屏幕尺寸。

但是在执行这些步骤时我发现了一个错误:

  1. 我在视频播放期间暂停了我的应用
  2. 我改变方向
  3. 我“恢复”我的应用

首先发生的事情是:onConfigurationChanged() 被调用,而不是 onResume(),之后我的所有字段都应该被恢复!这会导致异常,其原因是除以零 - 某些字段等于 0,因为它尚未恢复。当然,这不是期望的行为。我的应用在以与暂停时相同的方向恢复时确实有效。

不过,我可以通过使用一个 bool 值指示我的 Activity 是否已“完全加载”——即是否已调用 onResume() 方法来避免这种情况。但我认为这不是一个非常干净的解决方案。

我的问题是:

  1. 这是有意为之的行为吗?我认为在 onResume() 之前调用 onConfigurationChanged() 很奇怪。这是错误吗?
  2. 我能否以编程方式“取消注册”我的应用程序以暂时监听 onConfigurationChanged()(例如,在 onPause() 方法中取消注册)?由于此方法不会被调用,因为您必须向它注册一些监听器,但会被调用,因为您在 AndroidManifest.xml 文件中放入了这一行:

    android:configChanges="方向"

最佳答案

http://developer.android.com/reference/android/app/Activity.html 中有 1 个答案

In some special cases, you may want to bypass restarting of your activity based on one or more types of configuration changes. This is done with the android:configChanges attribute in its manifest. For any types of configuration changes you say that you handle there, you will receive a call to your current activity's onConfigurationChanged(Configuration) method instead of being restarted. If a configuration change involves any that you do not handle, however, the activity will still be restarted and onConfigurationChanged(Configuration) will not be called.

所以在再次开始 Activity 之前调用 onconfigrationchanged 以允许更改是正常的。 2-我对此不确定..

关于Android - 暂停 Activity 后在 onResume() 之前调用 onConfigurationChanged(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9004002/

相关文章:

Eclipse中的Android配置问题

android - 使用 Android 拍照时出现问题(垂直相机 | 人像)

android - 带有图像的自定义标题

android - 如何更改相机 View 以聚焦多个标记

Android:任务 ':dexDebug' 执行失败

android - Recyclerview - 无法在滚动回调中调用此方法

java - 如何将我的 textClock 设置为手机的默认时间格式?

ANDROID:上下文菜单不显示

android - 使用单个 sprite 表而不是多个图像是否可以提高 android 中 opengl 渲染的性能?

android - 在开始新 Activity 之前显示进度对话框