更改方向时onConfigurationChanged不起作用

时间:2013-08-05 06:22:27

标签: android

活动的manaifest.xml如下:

<activity
    android:name="com.wd.wuc.SearchStatusActivity"
    android:label="@string/app_name"
    android:screenOrientation="sensor"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:configChanges="orientation|keyboard"  >
</activity>

活动的代码如下:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_status);
    System.out.println("onCreate!!");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    System.out.println("onConfigurationChanged!!");
}

当我改变方向时,我想运行onConfigurationChanged 但是代码只运行onCate方法,而不是onConfigurationChanged 我怎么修改它?

4 个答案:

答案 0 :(得分:5)

在文档(http://developer.android.com/guide/topics/manifest/activity-element.html)中,您可以找到:

If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations.

如果您的目标是API 13或更高版本,则应尝试将“| screenSize”添加到configChanges:

android:configChanges="orientation|keyboard|screenSize"

答案 1 :(得分:1)

android:configChanges="orientation|keyboard"

这一行告诉Dalvik编译器你正在处理键盘和Orientation的更改。所以请删除这一行,它肯定会起作用。

答案 2 :(得分:1)

替换

android:configChanges="orientation|keyboard"

android:configChanges="orientation|keyboardHidden"

在您的清单文件中。

查看this以供参考。

答案 3 :(得分:0)

从清单文件中删除此标记。然后它会工作

android:configChanges="orientation|keyboard"