Saturday, 27 October 2018

onConfigurationChanged for View in WindowManager not working reliable

I'm adding a View to the WindowManager and overwrite it's onConfigurationChanged function like following:

View Code

// onConfigurationChanged should be called after config change has finished
override fun onConfigurationChanged(newConfig: Configuration) {
    super.onConfigurationChanged(newConfig)
    // also tried executing following in the `GlobalLayoutListener` without any difference
    onConfigOrSystemUIChanged(newConfig)
}

fun onConfigOrSystemUIChanged(newConfig: Configuration?) {
    val screen = getScreenSize(this)

    // screen should hold current screen size
    // but this sometimes contains the wrong size, the one from before the config change

    L.d("[%s] Config changed (%s)", logBaseInfo, screen)
}

Service Code

override fun onConfigurationChanged(newConfig: Configuration) {
    super.onConfigurationChanged(newConfig)

    val screen = getScreenSize(this)
    L.d("Config changed in SERVICE: Screen: %s | newConfig: %s", screen, newConfig);
}

Problem

I can see that this works about 99% of the time. I rotate my device, turn it on, off and so on and everything works fine. But every now and then I see that it fails once, meaning my view reads the wrong screen size after the device has been rotated. Any ideas why?

Additonal info - logs

I added some logs to my service's onConfigurationChanged and to my view's onConfigurationChanged. I have 5 views added to the WindowManager and see following:

[OverlayService:168 onConfigurationChanged]: Config changed in SERVICE: Screen: Point(2076, 1080) | newConfig: {0 1.0 themeSeq = 0 showBtnBg = 0 232mcc1mnc [de_AT] ldltr sw360dp w692dp h336dp 480dpi nrml long hdr land finger -keyb/v/h -nav/h appBounds=Rect(144, 0 - 2220, 1080) s.839 mkbd/h desktop/d ?dc}
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 24]] Config changed (Point(2076, 1080))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 26]] Config changed (Point(2076, 1080))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 52]] Config changed (Point(2076, 1080))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 53]] Config changed (Point(2076, 1080))
=> [BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 65]] Config changed (Point(1080, 2076))
// ... here some time that wents by ...
[OverlayService:168 onConfigurationChanged]: Config changed in SERVICE: Screen: Point(1080, 2076) | newConfig: {0 1.0 themeSeq = 0 showBtnBg = 0 232mcc1mnc [de_AT] ldltr sw360dp w360dp h668dp 480dpi nrml long hdr port finger -keyb/v/h -nav/h appBounds=Rect(0, 0 - 1080, 2076) s.840 mkbd/h desktop/d ?dc}
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 26]] Config changed (Point(1080, 2076))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 52]] Config changed (Point(1080, 2076))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 53]] Config changed (Point(1080, 2076))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 65]] Config changed (Point(1080, 2076))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 24]] Config changed (Point(1080, 2076))

Here you see that one view (marked with => at the beginning of the log line, the 6th line) does get the wrong screen size and I don't know why...

Additonal info - functions

fun getScreenSize(context: Context) {
    val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    val outSize = Point()
    wm.getDefaultDisplay().getSize(outSize)
    return outSize
}



from onConfigurationChanged for View in WindowManager not working reliable

No comments:

Post a Comment