Implemented checking for unsaved changes on back button press

This commit is contained in:
dankito 2020-10-22 22:03:09 +02:00
parent d73e359c27
commit 9e072f1d5e
1 changed files with 21 additions and 1 deletions

View File

@ -1,5 +1,6 @@
package net.dankito.banking.ui.android.dialogs.settings package net.dankito.banking.ui.android.dialogs.settings
import android.app.Dialog
import android.content.DialogInterface import android.content.DialogInterface
import android.os.Bundle import android.os.Bundle
import android.view.* 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) { protected open fun setupToolbar(toolbar: Toolbar, dialogTitle: String, showSaveButton: Boolean = true) {
toolbar.apply { toolbar.apply {
title = dialogTitle title = dialogTitle
@ -103,7 +123,7 @@ abstract class SettingsDialogBase : DialogFragment() {
} }
override fun onDismiss(dialog: DialogInterface) { override fun onDismiss(dialog: DialogInterface) {
log.info("Dismissung Fragment $this") log.info("Dismissing Fragment $this")
super.onDismiss(dialog) super.onDismiss(dialog)
} }