android - unexpected brief orientation change at switch of activity
<p>I want to set the orientation of the screen dynamically in my android application. For that I use </p>

<pre><code>activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT));
</code></pre>

<p>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.</p>

<p>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.</p>

<p>Any suggestion how to avoid this unwanted occasional screen orientation switch would be great.</p>

<p>Thanks</p>

<p>martin</p>

<pre><code>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);

}
}  
</code></pre>

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

<p>The layouts are as simple as </p>

<pre><code>&lt;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" &gt;

&lt;Button
    android:id="@+id/button_a"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="Click A" /&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p>and the manifest </p>

<pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.switchactivitytest"
android:versionCode="1"
android:versionName="1.0" &gt;

&lt;uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" /&gt;

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

            &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;
    &lt;activity
        android:name=".B_Activity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label="B Activity" &gt;
    &lt;/activity&gt;
&lt;/application&gt;

&lt;/manifest&gt;
</code></pre>
