java Activity 中未调用 Android onConfigurationChanged()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6359328/
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() is not being called in Activity
提问by littleK
I realize that there are a couple other posts on this topic, however the solutions for those posts are not working for me.
我意识到还有一些关于这个主题的其他帖子,但是这些帖子的解决方案对我不起作用。
Basically, I want to cease my Activity from restarting upon a device orientation change. To do this, I have modified the activity in the manifest file:
基本上,我想在设备方向更改时停止我的 Activity 重新启动。为此,我修改了清单文件中的活动:
<activity android:name=".MyActivity" android:configChanges="orientation|keyboardHidden"></activity>
and I have overridden onConfigurationChanged()
in my Activity:
我onConfigurationChanged()
在我的活动中覆盖了:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
System.out.println("IN onConfigurationChanged()");
}
However, the activity is still restarting upon an orientation change, and the onConfigurationChanged()
method is not being called.
但是,活动仍会在方向更改时重新启动,并且onConfigurationChanged()
不会调用该方法。
Does anyone know why this may be happening?
有谁知道为什么会发生这种情况?
采纳答案by littleK
The only thing that worked was using getLastNonConfigurationInstance(). http://developer.android.com/reference/android/app/Activity.html#getLastNonConfigurationInstance()
唯一有效的是使用 getLastNonConfigurationInstance()。http://developer.android.com/reference/android/app/Activity.html#getLastNonConfigurationInstance()
回答by leonvian
You should use 13 API and set this configuration in your activity's part of the manifest: android:configChanges="orientation|keyboardHidden|screenSize"
您应该使用 13 API 并在清单的活动部分设置此配置:android:configChanges="orientation|keyboardHidden|screenSize"
It works fine. At all Android version.
它工作正常。在所有的Android版本。
回答by Avinash Agrawal
Change your manifest to following
将您的清单更改为以下
<activity android:name=".MyActivity" android:configChanges="orientation|keyboardHidden|screenSize"></activity>
and refer this link for detailed explanation orientation issue
并参考此链接详细说明方向问题
回答by Jaber Shabeek
You should not use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); method call any where in your application this will avoid calling onConfigChanged() method.
你不应该使用 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 方法调用应用程序中的任何位置,这将避免调用 onConfigChanged() 方法。
回答by DArkO
if you define configchanges=orientation in your manifest then the activity won't restart but instead onConfigurationChanged will be call as you have currently have it implemented. First try to log that with the log class Log (this is the proper way to log things in android don't use System out for this its considered a bad practice) and before super but that is just a 1% chance it will fix what is happening to you.
如果您在清单中定义 configchanges=orientation 则活动将不会重新启动,而是将调用 onConfigurationChanged ,因为您目前已实现它。首先尝试使用日志类 Log 记录它(这是在 android 中记录事物的正确方法,不要使用 System out,因为这被认为是一种不好的做法),然后在 super 之前,但只有 1% 的机会可以解决问题正在发生在你身上。
The second case is that you have the current activity nested in a tabHost for example or Activity Group. if your activity has a parent activity then configuration change needs to be added in that one and the callback will happen there.
第二种情况是您将当前活动嵌套在例如 tabHost 或活动组中。如果您的活动具有父活动,则需要在该活动中添加配置更改,并且回调将在那里发生。
If that is the case and you want to forward the result or also do something in the child then you need to get a reference to the child in the parent and call a method on it for the changes.
如果是这种情况,并且您想转发结果或在子进程中执行某些操作,那么您需要在父进程中获取对子进程的引用,并在其上调用一个方法来进行更改。
回答by Edmund Chang
if you have a fragment then you need this also:
如果你有一个片段,那么你也需要这个:
void setRetainInstance(boolean retain)
Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change).
控制是否在 Activity 重新创建期间保留片段实例(例如来自配置更改)。
i ran into this and setting it to 'true' fixed it.
我遇到了这个并将其设置为“true”修复了它。
回答by mohsin
I used this, and it helped:
我使用了这个,它有帮助:
package="com.s2dio.evallet"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
回答by user1531015
Modefiy your onConfigurationChanged
method to the following
将您的onConfigurationChanged
方法修改为以下内容
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}