Android方向故障?(An Android orientation glitch?)
我相信在Android的方向处理中偶然发现了一个固有的故障。
我有一个活动A.
setContentView(R.layout.activity_main); setRequestedOrientation(ActivityInfo.SCREE_ORIETATIO_OSESOR);没有传感器意味着活动A不会受到物理设备旋转的影响。
我还有一个活动B,它有一个重写的onSaveInstanceBundle
/*Will be called before orientation change*/ @Override protected void onSaveInstanceState(Bundle currentState) { (currentState); currentState.putBoolean(ApplicationCtants.Messages.ORIETATIO_CHAGED , true); currentState.putBoolean("RESTART", true); currentState.putString(ApplicationCtants.Messages.CURRET_LAGUAGE,currentLocale); }当屏幕方向改变时,总是调用onSaveInstance。 像A一样,B在onCreate()下也有以下内容
setContentView(R.layout.activity_main); setRequestedOrientation(ActivityInfo.SCREE_ORIETATIO_OSESOR);现在出现了这个难题。
我从A按下一个按钮,当我按正常纵向模式按住设备时,该按钮被编程为调用B. 我有一个断点连接到onSaveInstance
正如预期的那样,调用B时不会调用onSaveInstance
然而,这是一个奇怪的事实。 如果我将设备保持在横向模式,然后调用B,则调用B的onSaveInstance,并且当程序在断点处暂停时,我们可以看到设备屏幕已经进行了美化。
因此即使我对活动没有传感器参数,传感器实际上仍在工作。 在活动开关IF之间,当我们将设备保持在风景中时,目标活动将以横向开始。 在让断点离开后,B恢复到纵向视图。
关于为什么这会影响我正在开发的项目,我有一个很长的故事。 我想知道是否有人对这种情况有任何见解。
总之,我的问题是:
在活动切换期间,Android系统是否忽略指定的方向,并且默认情况下使用设备的物理位置? 我无法在androidManifest中设置方向。 我已经注意到如果我们将定向模式放在清单中就不是这种情况。
I believe to have stumbled upon an inherent glitch in the orientation handling of Android.
I have an activity A
setContentView(R.layout.activity_main); setRequestedOrientation(ActivityInfo.SCREE_ORIETATIO_OSESOR);o sensor implies that the Activity A won't be affected by the physical device rotation.
I also have an activity B which has an overridden onSaveInstanceBundle
/*Will be called before orientation change*/ @Override protected void onSaveInstanceState(Bundle currentState) { (currentState); currentState.putBoolean(ApplicationCtants.Messages.ORIETATIO_CHAGED , true); currentState.putBoolean("RESTART", true); currentState.putString(ApplicationCtants.Messages.CURRET_LAGUAGE,currentLocale); }onSaveInstance is always called when the orientation of the screen is changed. Like A, B also has the following under onCreate()
setContentView(R.layout.activity_main); setRequestedOrientation(ActivityInfo.SCREE_ORIETATIO_OSESOR);ow here comes the conundrum.
I press a button from A, which is programmed to invoke B while I am holding the device in normal portrait mode. I have a breakpoint attached to onSaveInstance
As expected, onSaveInstance is not called when B is invoked
However, here is a strange fact. If I hold the device in landscape mode, and then invoke B, B's onSaveInstance is invoked, and as the program is suspended upon the breakpoint, we can see that the device screen has landscaped.
Hence even though I have O SESOR argument on the activity, the sensor is in fact still working. And between an activity switch IF DOE WHILE WE HOLD THE DEVICE I LADSCAPE, the target activity starts in landscape. After I let the breakpoint go, B resumes to portrait view.
I have a long story as to why this is affecting a project that I am developing. I wished to know if anyone has any insights into this situation.
To sum up, my question is:
Is it true that during an activity switch, the Android system ignores the specified orientation, and by default uses the device's physical position? I can't set the orientation in the androidManifest. I have noted this is not the case if we put the orientation mode in the manifest.
最满意答案
编辑:留下以下参考,但这显然不起作用。 :(
他希望应用程序在手机上锁定为肖像,并可以在平板电脑上自由旋转。
啊。 那么,你实际上可以通过清单来实现这一点。 你如何做到这取决于你想要定义为“平板电脑”。 让我们假设截止值是Google建议的sw600dp (最小宽度600dp是7英寸平板电脑的一个很好的断点)。
您可以在res文件夹中创建以下文件结构:
res - values - styles.xml - values-sw600dp - styles.xml在您的values/styles.xml (这将是您的非平板电脑样式),为您的Activity定义以下样式:
<!-- This will be a base style that will affect both tablet and non-tablet --> <style name="BaseTheme" parent="{{pick a base theme here -- e.g. Theme.Light}}"/> <!-- This is the local style that will be used on devices whose smallest width is less than 600dp --> <style name="BaseTheme.Activity"> <item name="android:screenOrientation">portrait</item> </style>在你的values-sw600dp/styles.xml :
<!-- This is the local style that will be used on devices whose smallest width is greater than or equal to 600dp. We define nothing here other than the fact that the style exists, so it will have the default screen orientation - that is, it will rotate freely. --> <style name="BaseTheme.Activity"/>现在,在您的AndroidManifest.xml ,在您的activity标记内添加:
android:theme="@style/BaseTheme.Activity"你应该得到理想的结果。 但是,您应首先删除您添加的所有Java代码,以尝试自行管理,因为它可能会与此冲突导致意外结果。
EDIT: Leaving the below for reference, but this clearly does OT actually work. :(
He wants the application to be locked to portrait on cellphones, and be free to rotate on tablets.
Ugh. Well, you actually can accomplish this through the manifest. How you do it depends on what you want to define as a "tablet". Let's assume the cutoff is the Google-suggested sw600dp (smallest width of 600dp is a good breakpoint for a 7" tablet).
You can make the following file structure in your res folder:
res - values - styles.xml - values-sw600dp - styles.xmlIn your values/styles.xml (this will be your non-tablet style), define the following styles for your Activity:
<!-- This will be a base style that will affect both tablet and non-tablet --> <style name="BaseTheme" parent="{{pick a base theme here -- e.g. Theme.Light}}"/> <!-- This is the local style that will be used on devices whose smallest width is less than 600dp --> <style name="BaseTheme.Activity"> <item name="android:screenOrientation">portrait</item> </style>In your values-sw600dp/styles.xml:
<!-- This is the local style that will be used on devices whose smallest width is greater than or equal to 600dp. We define nothing here other than the fact that the style exists, so it will have the default screen orientation - that is, it will rotate freely. --> <style name="BaseTheme.Activity"/>ow, in your AndroidManifest.xml, inside your activity tag, add:
android:theme="@style/BaseTheme.Activity"And you should have the desired result. However, you should first remove all of the Java code you added to try to manage this yourself, as it will likely conflict with this causing unexpected results.
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
推荐阅读
留言与评论(共有 13 条评论) |
本站网友 李焱 | 15分钟前 发表 |
我想知道是否有人对这种情况有任何见解 | |
本站网友 脐带的意思 | 8分钟前 发表 |
the target activity starts in landscape. After I let the breakpoint go | |
本站网友 石斛不适宜人群 | 13分钟前 发表 |
EDIT | |
本站网友 95587 | 6分钟前 发表 |
我从A按下一个按钮 | |
本站网友 苏州龙景花园 | 12分钟前 发表 |
这是一个奇怪的事实 | |
本站网友 伤透心 | 30分钟前 发表 |
true); currentState.putBoolean("RESTART" | |
本站网友 三根汤 | 0秒前 发表 |
add | |
本站网友 徐州提香湾 | 20分钟前 发表 |
inside your activity tag | |
本站网友 首城国际二手房 | 14分钟前 发表 |
inside your activity tag | |
本站网友 超薄避孕套 | 0秒前 发表 |
但是 | |
本站网友 强网杯 | 2分钟前 发表 |
我有一个活动A. setContentView(R.layout.activity_main); setRequestedOrientation(ActivityInfo.SCREE_ORIETATIO_OSESOR); 没有传感器意味着活动A不会受到物理设备旋转的影响 | |
本站网友 wooha | 9分钟前 发表 |
and then invoke B |