From 9e072f1d5ee125b03f925b33e9f8faf571d62bf1 Mon Sep 17 00:00:00 2001 From: dankito Date: Thu, 22 Oct 2020 22:03:09 +0200 Subject: [PATCH] Implemented checking for unsaved changes on back button press --- .../dialogs/settings/SettingsDialogBase.kt | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/ui/BankingAndroidApp/src/main/java/net/dankito/banking/ui/android/dialogs/settings/SettingsDialogBase.kt b/ui/BankingAndroidApp/src/main/java/net/dankito/banking/ui/android/dialogs/settings/SettingsDialogBase.kt index 25588503..a21f6ffd 100644 --- a/ui/BankingAndroidApp/src/main/java/net/dankito/banking/ui/android/dialogs/settings/SettingsDialogBase.kt +++ b/ui/BankingAndroidApp/src/main/java/net/dankito/banking/ui/android/dialogs/settings/SettingsDialogBase.kt @@ -1,5 +1,6 @@ package net.dankito.banking.ui.android.dialogs.settings +import android.app.Dialog import android.content.DialogInterface import android.os.Bundle import android.view.* @@ -45,6 +46,25 @@ abstract class SettingsDialogBase : DialogFragment() { } + override fun setupDialog(dialog: Dialog, style: Int) { + super.setupDialog(dialog, style) + + dialog.setOnKeyListener { _, keyCode, event -> + if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_UP) { + return@setOnKeyListener handleBackButtonPress() + } + + false + } + } + + protected open fun handleBackButtonPress(): Boolean { + askToDismissChanges() + + return hasUnsavedChanges + } + + protected open fun setupToolbar(toolbar: Toolbar, dialogTitle: String, showSaveButton: Boolean = true) { toolbar.apply { title = dialogTitle @@ -103,7 +123,7 @@ abstract class SettingsDialogBase : DialogFragment() { } override fun onDismiss(dialog: DialogInterface) { - log.info("Dismissung Fragment $this") + log.info("Dismissing Fragment $this") super.onDismiss(dialog) }