一个Android定位故障? [英] An android orientation glitch?

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

问题描述

我相信在Android中的方向处理一个固有的故障已经迷迷糊糊。

I believe to have stumbled upon a inherent glitch in the orientation handling of android .

我有一个活性的研究A

I have an acitivity A

 setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

没有传感器意味着活性的不会受到物理设备旋转的影响。

No sensor implies that the Activity A wont be affected by the physical device rotation .

我也有它有一个覆盖onSaveInstanceBundle的活动B

I also have an activity B which has an overridden onSaveInstanceBundle

/*Will be called before orientation change*/
@Override
protected void onSaveInstanceState(Bundle currentState) {
      super.onSaveInstanceState(currentState);
      currentState.putBoolean(ApplicationConstants.Messages.ORIENTATION_CHANGED , true);
      currentState.putBoolean("RESTART", true);

      currentState.putString(ApplicationConstants.Messages.CURRENT_LANGUAGE,currentLocale);

    }

当在屏幕的方向改变onSaveInstance总是被调用。
另外像A,B也有根据的​​OnCreate下列()

onSaveInstance is always called when the orientation of the screen is changed . Also like A , B also has the following under oncreate()

setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

现在来这里的难题。

我preSS来自A的按钮,该按钮编程调用b在我举着正常potrait模式下的设备。我有连接到onSaveInstance断点

I press a button from A , which is programmed to invoke B while I am holding the device in normal potrait mode. I have a breakpoint attached to onSaveInstance

正如预期的那样,当B调用onSaveInstance不叫

As expected , onSaveInstance is not called when B is invoked

然而,这是一个奇怪的事实。如果我持有在横向模式下的设备,然后调用B,B的onsaveInstance被调用,并作为程序在断点处暂停,我们可以看到,手机屏幕拥有园景。

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 .

因此​​,即使我有没有传感器参数上的活动,传感器是逸岸仍在工作。和一个活动开关,如果DONE虽然我们器件保持景观之间,目标活动开始的风景线。我让断点进去后,B恢复到potrait视图

Hence even though I have NO SENSOR argument on the activity , the sensor is infact still working. And between an activity switch IF DONE WHILE WE HOLD THE DEVICE IN LANDSCAPE , the target activity starts in landscape . After I let the breakpoint go , B resumes to potrait 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

这是真的,一个活动开关时,Android系统会忽略指定的方向,并默认使用该设备的物理位置? 。我不能设置在androidmanifest方向。我注意到,这是不是这样,如果我们把方向模式在清单中。

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.

感谢您,

推荐答案

编辑:离开下方作参考,但这显然不不会实际工作。 (

Leaving the below for reference, but this clearly does NOT actually work. :(

他希望应用程序被锁定到肖像在手机上,并
  可以自由地在平板电脑上旋转。

He wants the application to be locked to portrait on cellphones, and be free to rotate on tablets.

唉。那么,你实际上可以通过舱单做到这一点。你如何做到这一点取决于你要定义为平板电脑的内容。让我们假设截止是谷歌与建议 sw600dp (的600dp的最小宽度是7平板电脑良好的断点)。

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).

您可以在水库以下文件结构文件夹:

res
    - values
        - styles.xml
    - values-sw600dp
        - styles.xml

在你的价值观/ styles.xml (这将是你的非平板式的),定义如下样式的活动

In 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>

在你的价值观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"/>

现在,在你的的Andr​​oidManifest.xml ,您活动里面标签中,添加:

Now, in your AndroidManifest.xml, inside your activity tag, add:

android:theme="@style/BaseTheme.Activity"

和应该具有所期望的结果。然而,你应该先除去所有的Java $ C $的c您加入到尝试管理这个自己,因为它会随着这会造成意想不到的结果有可能发生冲突。

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.

这篇关于一个Android定位故障?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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