2

I am trying to start a new activity when an Android device is rotated, but I don;t even seem to be detecting the rotation in the emulator.

I've read the thread at Android: listen for Orientation change? and that all seems to amke sense, but its just not working.

In my manifest I have:

 <activity android:name=".MainActivity" android:configChanges="orientation|screenSize" android:screenOrientation="portrait" android:windowSoftInputMode="stateHidden"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 

and in my mainActivity.java I have:

@Override public void onConfigurationChanged (Configuration newConfig) { super.onConfigurationChanged(newConfig); int orientation=newConfig.orientation; switch(orientation) { case Configuration.ORIENTATION_LANDSCAPE: showMessage("landscape"); break; case Configuration.ORIENTATION_PORTRAIT: showMessage("portrait"); break; } } 

This obviously won't start the new Activity, but I am trying to get the orientation detection working first (showMessage just calls a Toast and is working elsewhere in my code, so that's not why I am not seeing anything).

When I run this in the emulator and use the rotate buttons, the emulator rotates as expected but I never see the Toast...

Where am I going wrong? (I am importing android.content.res.Configuration as required for the Configuration constants).

2 Answers 2

1

onConfigurationChanged method will not be invoked when you rotate your device, actually, nothing will be invoked because of this line:

android:screenOrientation="portrait" 

device is just locked in a portrait mode, remove this line, and onConfigurationChanged method should be invoked.

If you want to detect that device is rotated and keep android:screenOrientation="portrait" line you can use accelerometer sensor.

Sign up to request clarification or add additional context in comments.

3 Comments

Ah.. I did wonder about that, but if I remove it will Android rotate the first Activity before it has a chance to start the second one?
@FatMonk It will happen simultaneously, first activity will be rotated and the second one will be launched at the same moment.
@VasylGlodan do you have a snippet to use the accelerometer sensor to detect rotation?
0

Try after removing this lines

android:configChanges="orientation|screenSize" android:screenOrientation="portrait" 

because you are locking screen orientation in manifest and you are also overriding on config change in activity.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.