5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

android:screenOrientation="sensorLandscape"としたときにonConfigurationChangedが呼ばれない

Posted at

androidアプリの画面の向きを横向きのみで縦に回転させたくないときには、
AndroidManifest.xml内にて

AndroidManifest.xml
<activity android:screenOrientation="sensorLandscape">

のように記述すると実現出来ます。

また、画面回転時のイベントを受け取るためには、

AndroidManifest.xml
<activity configChanges="orientation|keyboardHidden|screenSize">

このように記述します。
(screenSizeはandroid3.2以上用)

しかし上記のように記述しても、
一部の端末で画面を回転させた時に、
ActivityのonConfigurationChangedイベントが呼ばれないことがありました。

そのため、代替手段として以下のメソッドを実装してイベントを取得しました。

SampleActivity.java
public class SampleActivity extends Activity implements SensorEventListener
{
	private int mRotation;
	private SensorManager mSensorManager;

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

		mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		mSensorManager.registerListener(this, 
			mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), 
			SensorManager.SENSOR_DELAY_NORMAL);
	}

	public void onSensorChanged(SensorEvent event) {
		int rotation = getWindowManager().getDefaultDisplay().getRotation();
		if (mRotation != rotation) {
			// 画面が回転した!
		}
	}
}
5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?