Android的onConfigurationChanged不会被调用Android、onConfigurationChanged

2023-09-11 20:37:07 作者:菠蘿蜜

我有麻烦告诉机器人不叫的onCreate()时的方向变化。我已经加入安卓configChanges =定向来我的表现,但仍时的方向变化的onCreate()被称为。这是我的code。

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.

推荐答案

几件事情尝试:

安卓configChanges =定位| keyboardHidden |屏幕尺寸,而不是安卓configChanges =定向

确认你是不是叫 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.

确认你没有使用机器人:screenOrientation 在清单

如果没有这样的作品,仔细阅读有关处理运行时更改了Android文档,并确保你正确地做的一切。可能有一些其他地方在code这是造成问题。 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.

 
精彩推荐