@@ -18,12 +18,11 @@ package com.ichi2.anki.dialogs
1818
1919import android.annotation.SuppressLint
2020import android.app.Dialog
21- import android.content.DialogInterface
2221import android.net.Uri
2322import android.os.Bundle
2423import android.os.Message
2524import android.os.Parcelable
26- import android.view.KeyEvent
25+ import androidx.activity.addCallback
2726import androidx.annotation.CheckResult
2827import androidx.annotation.StringRes
2928import androidx.appcompat.app.AlertDialog
@@ -269,15 +268,7 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
269268 }
270269 }
271270 }
272- alertDialog
273- .setOnKeyListener { _: DialogInterface ? , keyCode: Int , _: KeyEvent ? ->
274- if (keyCode == KeyEvent .KEYCODE_BACK ) {
275- Timber .i(" DIALOG_RESTORE_BACKUP caught hardware back button" )
276- requireActivity().dismissAllDialogFragments()
277- return @setOnKeyListener true
278- }
279- false
280- }.create()
271+ alertDialog.create()
281272 }
282273 DIALOG_NEW_COLLECTION -> {
283274 // Allow user to create a new empty collection
@@ -403,6 +394,22 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
403394 }
404395 }
405396
397+ override fun setupDialog (
398+ dialog : Dialog ,
399+ style : Int ,
400+ ) {
401+ super .setupDialog(dialog, style)
402+
403+ if (requireDialogType() == DIALOG_RESTORE_BACKUP ) {
404+ // we don't want to go back to DIALOG_CONFIRM_RESTORE_BACKUP if back is pressed
405+ // instead, close all dialogs and return to the DeckPicker
406+ (dialog as AlertDialog ).onBackPressedDispatcher.addCallback(this , true ) {
407+ Timber .i(" DIALOG_RESTORE_BACKUP caught hardware back button" )
408+ requireActivity().dismissAllDialogFragments()
409+ }
410+ }
411+ }
412+
406413 /* * @see DeckPicker.showDatabaseErrorDialog */
407414 private fun showDatabaseErrorDialog (errorDialogType : DatabaseErrorDialogType ) {
408415 requireDeckPicker().showDatabaseErrorDialog(errorDialogType, exceptionData)
0 commit comments