As Display methods are deprecated in Android 12, I am planning to use Jetpack's backward compatible WindowManager library succeeding Display.
However I am not sure whether I face an issue if I directly access the sizes of a screen in Activity like below:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val windowMetrics = WindowMetricsCalculator.getOrCreate()
.computeCurrentWindowMetrics(this@WindowMetricsActivity)
val width = windowMetrics.bounds.width()
val height = windowMetrics.bounds.height()
}
Because Google's sample code insists using onConfigurationChanged
method by using a utility container view like below:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Add a utility view to the container to hook into
// View.onConfigurationChanged.
// This is required for all activities, even those that don't
// handle configuration changes.
// We also can't use Activity.onConfigurationChanged, since there
// are situations where that won't be called when the configuration
// changes.
// View.onConfigurationChanged is called in those scenarios.
// https://issuetracker.google.com/202338815
container.addView(object : View(this) {
override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
logCurrentWindowMetrics("Config.Change")
}
})
}
@SuppressLint("NotifyDataSetChanged")
private fun logCurrentWindowMetrics(tag: String) {
val windowMetrics = WindowMetricsCalculator.getOrCreate()
.computeCurrentWindowMetrics(this@WindowMetricsActivity)
val width = windowMetrics.bounds.width()
val height = windowMetrics.bounds.height()
adapter.append(tag, "width: $width, height: $height")
runOnUiThread {
adapter.notifyDataSetChanged()
}
}
In our project, we directly access the sizes of screens and I am wondering how we migrate to accessing them after onConfigurationChanged
invokes and emits the size values by using MAD skills.
Any approaches will be appreciated