Android Activity Show AlertDialog for Close Confirmation on Back Pressed

July 24, 2018
class PasswordActivity : AppCompatActivity() {
    private var needSave: Boolean = false

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return true
    }

    override fun onBackPressed() {
        AlertDialog.Builder(this)
            .setMessage("Close?")
            .setPositiveButton(android.R.string.ok) { dialog, whichButton ->
                super.onBackPressed()
            }
            .setNegativeButton(android.R.string.cancel) { dialog, whichButton ->

            }
            .show()
    }
}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.