Skip to content

Commit 7f5a36a

Browse files
david-allisonlukstbit
authored andcommitted
improvement(backup): use back callback
learning: ComponentDialog.onBackPressedDispatcher is required for dialogs, as requireActivity().onBackPressedDispatcher doesn't fire Issue 14558 Co-authored-by: lukstbit <52494258+lukstbit@users.noreply.github.com>
1 parent a459da0 commit 7f5a36a

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/dialogs/DatabaseErrorDialog.kt

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ package com.ichi2.anki.dialogs
1818

1919
import android.annotation.SuppressLint
2020
import android.app.Dialog
21-
import android.content.DialogInterface
2221
import android.net.Uri
2322
import android.os.Bundle
2423
import android.os.Message
2524
import android.os.Parcelable
26-
import android.view.KeyEvent
25+
import androidx.activity.addCallback
2726
import androidx.annotation.CheckResult
2827
import androidx.annotation.StringRes
2928
import 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

Comments
 (0)