【问题标题】:onConfigChanged() NOT called when device orientation changes设备方向改变时不调用 onConfigChanged()
【发布时间】:2012-06-10 08:41:27
【问题描述】:

我在捕捉应用中的方向变化时遇到问题。我想防止我的应用表单在设备重新定向时重新启动。

我的应用共有 3 个活动(第三个是 FragmentActivity)。主要的用户交互是通过 FragmentActivity。

我已更改清单,以便所有活动都具有以下属性:

android:configChanges="orientation"

我还在所有活动中重写了 onConfigurationChanged() 方法,并查看它是否真的被调用了,我包含了一个 Log.i() 语句,但没有任何报告。

我目前正在使用模拟器进行测试,当我执行 CTRL+F12 时会报告方向更改。

I/ActivityManager(67):配置更改:{ scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=1 layout=34 uiMode=17 序列=10}

我有什么遗漏吗?

【问题讨论】:

    标签: android android-orientation android-configchanges


    【解决方案1】:

    来自:http://developer.android.com/guide/topics/resources/runtime-changes.html

    Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

    【讨论】:

    • 我只是注意到它在实际设备上运行良好,即没有像我预期的那样重新启动。可能是模拟器问题?
    • 这意味着如果我的应用目标 api 级别是 16,我应该使用 android:configChanges="orientation|screenSize" ?
    • 我有 min sdk 版本 10 和目标 sdk 版本 15。Android 不允许我添加 screenSize 作为配置更改案例,也不会将屏幕方向更改委托给 Activity。将目标 sdk 版本更改为 10 会恢复预期的功能。关于我如何能够保持目标版本 15,但仍然能够捕获屏幕方向变化的任何想法?谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    相关资源
    最近更新 更多