未调用 Android onConfigurationChanged [英] Android onConfigurationChanged not being called

查看:41
本文介绍了未调用 Android onConfigurationChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法告诉 Android 在方向改变时不要调用 onCreate().我已经将 android:configChanges="orientation" 添加到我的清单中,但是当方向改变时 onCreate() 仍然被调用.这是我的代码.

I am having trouble with telling Android to not call onCreate() when the orientation changes. I have added android:configChanges="orientation" to my manifest but still when the orientation changes onCreate() is called. Here is my code.

AndroidManifest.xml

AndroidManifest.xml

<activity android:name="SearchMenuActivity" android:theme="@android:style/Theme.NoTitleBar" android:configChanges="orientation"></activity>

SearchMenuActivity.java

SearchMenuActivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set the current layout to the search_menu
    setContentView(R.layout.search_menu_activity);

    Log.d(TAG, "onCreate() Called");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    //don't reload the current page when the orientation is changed
    Log.d(TAG, "onConfigurationChanged() Called");
    super.onConfigurationChanged(newConfig);
}

还有我的 LogCat 输出

And my LogCat Output

06-23 12:33:20.327: DEBUG/APP(2905): onCreate() Called
//Orientation Changes
06-23 12:33:23.842: DEBUG/APP(2905): onCreate() Called

有谁知道我做错了什么?谢谢.

Does anyone know what I am doing wrong? Thanks.

推荐答案

尝试以下几点:

android:configChanges="orientation|keyboardHidden|screenSize" 而不是 android:configChanges="orientation"

确保您没有在任何地方调用 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);.这将导致 onConfigurationChange() 不会触发.

Ensure that you are not calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); anywhere. This will cause onConfigurationChange() to not fire.

检查您没有在清单中使用 android:screenOrientation.

Check that you are not using android:screenOrientation in your manifest.

如果这些都不起作用,请通读有关处理运行时更改的 Android 文档,并确保您所做的一切都正确无误.您的代码中可能有其他地方导致了问题.http://developer.android.com/guide/topics/resources/runtime-changes.html

If none of that works, read through the Android doc on handling runtime changes and make sure you are doing everything correctly. There may be something somewhere else in your code that's causing the problem. http://developer.android.com/guide/topics/resources/runtime-changes.html

正如 derrik 所指出的,我假设您正在通过加速度计检测设备面向的方向来更改配置.如果您希望在显示/隐藏键盘时更改配置,清单中的 configChanges 也必须包含 keyboardHidden.

As derrik pointed out, I assumed that you were changing the configuration with the accelerometer detecting what way the device was facing. If you want the configuration to change as the keyboard is shown/hidden the configChanges in the manifest must include keyboardHidden as well.

这篇关于未调用 Android onConfigurationChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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