Android的 - 意外的简短方向变化的活动开关 [英] android - unexpected brief orientation change at switch of activity

查看:99
本文介绍了Android的 - 意外的简短方向变化的活动开关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Andr​​oid应用程序动态设置屏幕的方向。对于我使用

  activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT));
 

和一致好评。这工作正常,到目前为止我还没有遇到偶尔取向不必要的短暂开关从一个活动到下一个要去的时候。当我把设备的方式这将让传感器设定方向不同的编程方式选中的人会出现问题。

我能够重现问题在下面这个简单的应用程序,它主要包括两个活动让通过pressing一个按钮,用户切换到其他活动。为简单起见,我将在活动中始终为横向的方向还请有记住,我需要的是选择的方向在运行时使移动方向设置到清单wouldnt为我做的。

任何建议如何避免这种不必要的偶然屏幕方向切换将是巨大的。

感谢

马丁

 公共类A_Activity延伸活动{

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    的setContentView(R.layout.main_a);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    按钮B =(按钮)findViewById(R.id.button_a);
    b.setOnClickListener(新OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){

            startActivity(新意图(A_Activity.this,B_Activity.class));
        }
    });

}

@覆盖
保护无效onResume(){
    // TODO自动生成方法存根
    super.onResume();

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

}
}
 

第二项活动 - B_Activity - 被定义只是开始A_Activity按钮preSS相同的方式

的布局简单,只要

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>

<按钮
    机器人:ID =@ + ID / button_a
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:文本=点击/>

< / LinearLayout中>
 

和清单

 <舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
包=com.example.switchactivitytest
安卓版code =1
机器人:VERSIONNAME =1.0>

<用途-SDK
    安卓的minSdkVersion =8
    机器人:targetSdkVersion =15/>

<应用
    机器人:图标=@可绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ AppTheme>
    <活动
        机器人:名称=。A_Activity
        机器人:configChanges =keyboardHidden |方向|屏幕尺寸
        机器人:标签=一个活动>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.MAIN/>

            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
    <活动
        机器人:名称=。B_Activity
        机器人:configChanges =keyboardHidden |方向|屏幕尺寸
        机器人:标签=B活动>
    < /活性GT;
< /用途>

< /舱单>
 

解决方案

使用此为您在清单中的活动,而不是编程方式更改方向

 安卓screenOrientation =风景
 

I want to set the orientation of the screen dynamically in my android application. For that I use

activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT));

and alike. That works fine so far yet I experience occasionally an unwanted brief switch of orientation when going from one activity to the next. The problem occurs when I hold the device in a manner which would let the sensor set the orientation differently to the programatically chosen one.

I was able to reproduce the problem in the following simple application which basically consists of two activities letting the user switch to the other activity through pressing a button. For the sake of simplicity I set the orientation in the activities always to landscape yet please have in mind that what I need is to choose the orientation at runtime so moving the orientation setting to the manifest wouldnt do for me.

Any suggestion how to avoid this unwanted occasional screen orientation switch would be great.

Thanks

martin

public class A_Activity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main_a);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    Button b = (Button) findViewById(R.id.button_a);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            startActivity(new Intent(A_Activity.this, B_Activity.class));
        }
    });

}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

}
}  

The second activity - B_Activity - is defined just in the same manner starting A_Activity on button press

The layouts are as simple as

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/button_a"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="Click A" />

</LinearLayout>

and the manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.switchactivitytest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".A_Activity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label="A Activity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".B_Activity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label="B Activity" >
    </activity>
</application>

</manifest>

解决方案

use this in for your activity in the manifest rather than programmatically change the orientation

android:screenOrientation="landscape"

这篇关于Android的 - 意外的简短方向变化的活动开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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