Java 未调用 Android onConfigurationChanged
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6457659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android onConfigurationChanged not being called
提问by Stefan Bossbaly
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.
我无法告诉 AndroidonCreate()
在方向改变时不要调用。我已添加android:configChanges="orientation"
到我的清单中,但仍然在onCreate()
调用方向更改时。这是我的代码。
AndroidManifest.xml
AndroidManifest.xml
<activity android:name="SearchMenuActivity" android:theme="@android:style/Theme.NoTitleBar" android:configChanges="orientation"></activity>
SearchMenuActivity.java
搜索菜单活动.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);
}
And my LogCat Output
还有我的 LogCat 输出
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.
有谁知道我做错了什么?谢谢。
采纳答案by shanet
A couple of things to try:
有几件事可以尝试:
android:configChanges="orientation|keyboardHidden|screenSize"
rather than android:configChanges="orientation"
android:configChanges="orientation|keyboardHidden|screenSize"
而不是 android:configChanges="orientation"
Ensure that you are not calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
anywhere. This will cause onConfigurationChange() to not fire.
确保您没有setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
在任何地方打电话。这将导致 onConfigurationChange() 不触发。
Check that you are not using android:screenOrientation
in your manifest.
检查您没有android:screenOrientation
在清单中使用。
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
如果这些都不起作用,请通读有关处理运行时更改的 Android 文档,并确保您正确执行所有操作。您的代码中可能有其他地方导致了问题。http://developer.android.com/guide/topics/resources/runtime-changes.html
EDIT: 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.
编辑:正如 derrik 所指出的,我假设您正在通过加速度计检测设备面向的方向来更改配置。如果您希望在显示/隐藏键盘时更改配置,清单中的 configChanges 也必须包括在内keyboardHidden
。
回答by derrik
You should change the configChanges entry in AndroidManifest.xml to:
您应该将 AndroidManifest.xml 中的 configChanges 条目更改为:
android:configChanges="keyboardHidden|orientation"
Otherwise, sliding the keyboard doesn't trigger onConfigurationChange() even though the orientation changes. I just tested this on my HTC Desire Z.
否则,即使方向发生变化,滑动键盘也不会触发 onConfigurationChange()。我刚刚在我的 HTC Desire Z 上测试了这个。
回答by nmr
This was my gremlin for the ~same problem:
这是我遇到相同问题的小鬼:
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).
注意:从 Android 3.2(API 级别 13)开始,当设备在纵向和横向之间切换时,“屏幕尺寸”也会发生变化。因此,如果在为 API 级别 13 或更高级别(由 minSdkVersion 和 targetSdkVersion 属性声明)开发时要防止由于方向更改而导致运行时重新启动,则除了“orientation”值之外,还必须包括“screenSize”值。也就是说,您必须声明 android:configChanges="orientation|screenSize"。但是,如果您的应用程序面向 API 级别 12 或更低级别,则您的 Activity 始终会自行处理此配置更改(此配置更改不会重新启动您的 Activity,即使在 Android 3.2 或更高版本的设备上运行时)。
(From http://developer.android.com/guide/topics/resources/runtime-changes.html)
(来自http://developer.android.com/guide/topics/resources/runtime-changes.html)
TL;DR: add "|screenSize" to android:configChanges="orientation" for API 14+
TL;DR:为 API 14+ 将“|screenSize”添加到 android:configChanges="orientation"
回答by sonida
Try use this one.....
试试用这个.....
android:configChanges="orientation|keyboardHidden|screenSize"
回答by Munir Basheer
Add this to your manifest to each activity.
将此添加到每个活动的清单中。
android:configChanges="keyboardHidden|orientation|screenSize"
Then, override onConfigurationChanged
on your activity as such
然后,覆盖onConfigurationChanged
您的活动
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
回答by craned
It wasn't triggering in my Fragment
(Fragments
aren't registered in the AndroidManifest.xml
) so I had to move it to the Activity
managing my Fragment
, in my case the TabsPagerActivity
.
这不是触发我的Fragment
(Fragments
未在注册的AndroidManifest.xml
),所以我不得不将它移动到Activity
管理我的Fragment
,在我的情况TabsPagerActivity
。
回答by Martin Pfeffer
Manifest:
显现:
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize">
</activity>
Activity & Fragment:
活动和片段:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.d(TAG, "onConfigurationChanged " +
(newConfig.orientation
== Configuration.ORIENTATION_LANDSCAPE
? "landscape" : "portrait"));
}
Output:
输出:
10-29 21:53:26.951 D/FragmentOne: onConfigurationChanged landscape 10-29 21:53:26.951 D/MainActivity:onConfigurationChanged landscape
10-29 21:53:26.951 D/FragmentOne: onConfigurationChanged landscape 10-29 21:53:26.951 D/MainActivity:onConfigurationChanged landscape
回答by anoopbryan2
Few things can be cross check:
很少有事情可以交叉检查:
In my case I was tracking language change of device using the onConfigurationChanged.
就我而言,我正在使用 onConfigurationChanged 跟踪设备的语言更改。
Few things need to know about onConfigurationChanged is there is some difference in behavious between onConfigurationChanged of Activity and Application.
关于 onConfigurationChanged 的一些事情需要了解的是 Activity 和 Application 的 onConfigurationChanged 之间的行为存在一些差异。
When you change the configuration of device the Application level onConfigurationChanged will be call automatically and immediately but onConfigurationChanged of activity will call when you navigate to that activity.
当您更改设备的配置时,应用程序级别的 onConfigurationChanged 将自动立即调用,但当您导航到该活动时,活动的 onConfigurationChanged 将调用。
and another thing is only locale in manifest will not work sometime So you have declare other config change event along with the locale(in my case) that should be like android:configChanges="layoutDirection|locale"
另一件事是清单中的语言环境有时不起作用所以你已经声明了其他配置更改事件以及应该像android:configChanges="layoutDirection|locale"的语言环境(在我的情况下)