I'm getting a crash that I don't understand how to fix. This is the stack trace
requestFeature() must be called before adding content
com.android.internal.policy.PhoneWindow in requestFeature at line 344
System
com.android.internal.app.AlertController in installContent at line 231
System
android.app.AlertDialog in onCreate at line 426
System
android.app.Dialog in dispatchOnCreate at line 425
System
android.app.Dialog in show at line 309
System
androidx.fragment.app.DialogFragment in onStart at line 728
System
androidx.fragment.app.Fragment in performStart at line 3162
System
androidx.fragment.app.FragmentStateManager in start at line 588
System
androidx.fragment.app.FragmentStateManager in moveToExpectedState at line 279
System
androidx.fragment.app.FragmentManager in executeOpsTogether at line 1890
System
androidx.fragment.app.FragmentManager in removeRedundantOperationsAndExecute at line 1814
System
androidx.fragment.app.FragmentManager in execPendingActions at line 1751
System
androidx.fragment.app.FragmentManager$5 in run at line 538
System
android.os.Handler in handleCallback at line 743
System
android.os.Handler in dispatchMessage at line 95
System
android.os.Looper in loop at line 150
System
android.app.ActivityThread in main at line 5621
System
java.lang.reflect.Method in invoke
System
com.android.internal.os.ZygoteInit$MethodAndArgsCaller in run at line 794
System
com.android.internal.os.ZygoteInit in main at line 684
I had previously been getting this crash and it pointed to a different line. It was pointing to this code in my code base:
fun android.app.AlertDialog.Builder.showAlertDialogWithDefaultInsets() {
create().apply {
window?.setDefaultWindowInsets()
show()
}
}
setDefaultWindowInsets is this function in my code base:
fun Window.setDefaultWindowInsets() {
ViewCompat.setOnApplyWindowInsetsListener(peekDecorView() ?: this.decorView) { view, windowInsets ->
view.updatePadding(30, 30, 30, 30)
WindowInsetsCompat.CONSUMED
}
}
This crash started happening when I made the app be edge to edge. Here is the code we implemented to accomplish that:
fun Activity.setEdgeToEdgeScreen() {
WindowCompat.setDecorFitsSystemWindows(window, false)
ViewCompat.setOnApplyWindowInsetsListener(window.peekDecorView()) { view, windowInsets ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemGestures())
var bottomInset = insets.bottom
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val gestureAvailable = window.peekDecorView().rootWindowInsets.getInsetsIgnoringVisibility(WindowInsets.Type.systemGestures()).left > 0
if (gestureAvailable) { // Gesture navigation mode
bottomInset = 0
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val gestureAvailable = window.peekDecorView().rootWindowInsets.systemGestureInsets.left > 0
if (gestureAvailable) { // Gesture navigation mode
bottomInset = 0
}
}
view.updatePadding(0, 90, 0, bottomInset)
WindowInsetsCompat.CONSUMED
}
}
fun Activity.setNavBarLight() {
ViewCompat.getWindowInsetsController(window.decorView)?.isAppearanceLightNavigationBars = true
window.navigationBarColor = ContextCompat.getColor(this, R.color.white)
}
fun Activity.setNavBarDark() {
ViewCompat.getWindowInsetsController(window.decorView)?.isAppearanceLightNavigationBars = true
window.navigationBarColor = ContextCompat.getColor(this, R.color.black)
}
fun Activity.setDarkStatusBar(customBgColor: Int = Color.BLACK) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.peekDecorView().windowInsetsController?.setSystemBarsAppearance(
0,
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
)
} else {
val flags = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or if (Build.VERSION.SDK_INT >= 26) View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR else 0
window.peekDecorView().systemUiVisibility = (window.peekDecorView().systemUiVisibility.inv() or flags).inv()
}
window.peekDecorView().setBackgroundColor(customBgColor)
}
fun Activity.setLightStatusBar(customBgColor: Int = Color.WHITE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.peekDecorView().windowInsetsController?.setSystemBarsAppearance(
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
)
} else {
val flags = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or if (Build.VERSION.SDK_INT >= 26) View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR else 0
window.peekDecorView().systemUiVisibility = window.peekDecorView().systemUiVisibility or flags
}
window.peekDecorView().setBackgroundColor(customBgColor)
}
Does anyone know how to fix this crash?