问题
So we can get for example safe inset top value (useful for devices which have notch/cutout):
override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val safeInsetTop = window.decorView.rootWindowInsets?.displayCutout?.safeInsetTop
if (DEBUG) Timber.d("onAttachedToWindow, safeInsetTop: $safeInsetTop")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (DEBUG) Timber.d("onCreate")
if (prefAlwaysLandscape) {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
if (resources.configuration.orientation != Configuration.ORIENTATION_LANDSCAPE) {
return // activity will be recreated
}
}
...
}
When it works normally (when you hold your phone straight - no angle):
Logs
onAttachedToWindow, safeInsetTop: 0
onCreate
When it works bad (when you hold your phone at some angle):
Logs
onAttachedToWindow, safeInsetTop: 112
onCreate
You can see there is 112 value instead of 0, which is stupid, in landscape orientation top safe value can't be more than 0, there is no notch, it's only possible for portrait orientation
So when the phone at angle and activity starts it thinks that it's in portrait orientation, but later configuration changes (to landscape) but I'm stuck with old 112 value, which is incorrect for landscape orientation
I don't know the solution for this issue
window.decorView.rootWindowInsets?.displayCutout?.safeInsetTop
can be called only from onAttachedToWindow
to return non null value
in onCreate
, onStart
or onResume
it will return null
...
Update
I found this windowManager.defaultDisplay.cutout?.safeInsetTop
from WindowInsets.getDisplayCutout is NULL everywhere except within onAttachedToWindow in fullscreen Java app
I can call it in onCreate
but still value is 112
when phone at angle
So it doesn't matter where you get it...
来源:https://stackoverflow.com/questions/65620959/incorrect-displaycutout-in-landscape-orientation-when-you-hold-your-device-at-so