Android onConfigurationChanged not called for Landscape and Reverselandscape

Override onConfigurationChanged method to detect orientation changes, but receiving event only when landscape change to portrait vise versa.

So when landscape changes to reverse landscape, onConfigurationChanged method can’t receive event.

onConfigurationChanged

How to use onConfigurationChanged method as follows.

Add android:configChanges in tag of AndroidManifest.xml.

AndroidManifest.xml
1
2
3
4
5
6
<application ... >
<activity
android:name=".MainActivity"
android:screenOrientation="sensorLandscape"
android:configChanges="orientation|screenSize|keyboardHidden"/>
</application>

Add Override function : onConfigurationChanged in MainActivity.java.

AndroidManifest.xml
1
2
3
override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
}

This method can’t figure out if it’s Landscape or Landscape-reverse.

But we can use Rotation change to figure out Landscape or Landscape-reverse.

Rotation Changed Listener

Step 1: Create callback : RotationCallback

Add kotlin interface : RotationCallback

RotationCallback.kt
1
2
3
interface RotationCallback {
fun onRotationChanged(lastRotation: Int, newRotation: Int)
}

Step 2: Create Listerner : RotationListener

Add kotlin class : RotationListener

mOrientationEventListener listened the rotation changes,and send event to top level by mCallback object.

RotationListener.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class RotationListener {
private var mOrientationEventListener: OrientationEventListener? = null
private var mCallback: RotationCallback? = null

private var lastRotation: Int = 0

constructor(callback: RotationCallback) {
mCallback = callback
}

fun listen(context: Context) {
mOrientationEventListener = object : OrientationEventListener(context, SensorManager.SENSOR_DELAY_NORMAL) {
override fun onOrientationChanged(orientation: Int) {
val localWindowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
if (null != localWindowManager && null != mCallback) {
val newRotation = localWindowManager.defaultDisplay.rotation
if (newRotation != lastRotation) {
mCallback!!.onRotationChanged(lastRotation, newRotation)
lastRotation = newRotation
}
}
}
}
mOrientationEventListener!!.enable()

lastRotation = (context.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay.rotation
}

fun stop() {
if (null != mOrientationEventListener) {
mOrientationEventListener!!.disable()
}
mOrientationEventListener = null
mCallback = null
}
}

Step 3: Register/Unregister listener

MainActivity implemented RotationCallback and onRotationChanged function,and setup rotation listener at onCreate step and unregister it at onDestroy step.

MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class MainActivity : AppCompatActivity(), RotationCallback {
private val TAG = "MainActivity "

private var mRotationListener : RotationListener? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

mRotationListener = RotationListener(this)
mRotationListener!!.listen(this.applicationContext)
}

override fun onDestroy() {
super.onDestroy()
Log.e(TAG, "onDestroy()")

mRotationListener!!.stop()
mRotationListener = null
}

//RotationCallback
override fun onRotationChanged(lastRotation: Int, newRotation: Int) {
Log.d(TAG, "onRotationChanged: last " + (lastRotation) +" new " + (newRotation));
}
}

By this method, it can figure out landscape and reverselandscape.

Android onConfigurationChanged not called for Landscape and Reverselandscape

https://nickcarter9.github.io/2019/11/21/2019/2019_11_21-reverselandscape-event/

Author

Nick Lin

Posted on

2019-11-21

Updated on

2023-01-18

Licensed under


Comments