Bugdroid
Bugdroid

Reputation: 329

Activity not launching when phone is held in landscape orientation

The activity launches and works well when phone is held in portrait orientation but when I launch my app by holding the phone in landscape, either the app doesn't start or screen goes black for a millisecond and nothing happens. The problem goes away when I remove this from the code

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

but it is essential as my application will only be supporting portrait. What could be the problem?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);
}

Upvotes: 2

Views: 112

Answers (3)

Chandan kushwaha
Chandan kushwaha

Reputation: 951

Remove this line

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

The above line will make the Activity to stay at Portrait mode. Just Add SensorPortrait mode.

android:screenOrientation="sensorPortrait" 

in manifest.

Upvotes: 3

Nauman Ali Shah
Nauman Ali Shah

Reputation: 163

add this line in your menifests activity tag .

<activity android:name=".SomeActivity"
    android:label="@string/app_name"
    android:screenOrientation="sensorPortrait" />

and remove

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

from your activity

Upvotes: 3

Rakesh
Rakesh

Reputation: 756

Remove this line from code

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

Add the orientation layout like this:

<activity android:name=".SomeActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait" />

Upvotes: 1

Related Questions