Reputation: 7794
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>
Upvotes: 1
Views: 317
Reputation: 1011
use this in for your activity in the manifest rather than programmatically change the orientation
android:screenOrientation="landscape"
Upvotes: 1