Home > Blockchain >  How can I handle back button pressed with a dialog fragment?
How can I handle back button pressed with a dialog fragment?

Time:12-05

I'm a total newbie, and my app has a main activity and a test-taking activity. Normally pressing back button in the test activity takes you back to main activity. This is fine, but I want to add a confirmation dialog asking if they really want to abandon the test first. So far I have the following in the test activity:

    override fun onBackPressed() {
        var exit = ExitTestDialogFragment()
        exit.show(supportFragmentManager,"exit")

    }

    class ExitTestDialogFragment : DialogFragment() {

        override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
            return activity?.let {
                val builder = AlertDialog.Builder(it)
                builder.setTitle("Leave Test?")
                builder.setMessage("Your score will be lost.")
                    .setPositiveButton("OK",
                        DialogInterface.OnClickListener { dialog, id ->
                            // This is where I'd like to return to Main Activity
                        })
                    .setNegativeButton("Cancel",
                        DialogInterface.OnClickListener { dialog, id ->
                            dialog.dismiss()// User cancelled the dialog
                        })
                // Create the AlertDialog object and return it
                builder.setCancelable(false)
                builder.create()
            } ?: throw IllegalStateException("Activity cannot be null")
        }
    }

I can't seem to figure out how how do what would normally be the Activity's super.onBackPressed() from the dialog fragment. Like I said, I'm super new to android, so may need a bit of an ELI5 answer.

CodePudding user response:

call finish() or this.finish() inside your DialogInterface.OnClickListener. Method finish() will destroy current activity that call it, in this case its test-taking activity

CodePudding user response:

You should call mainActivity from your dialog positive Button.

.setPositiveButton("OK",
                    DialogInterface.OnClickListener { dialog, id ->
                        // here you can get your current activity                         
                       //then dismiss your dialog and finish current activity
                       //call context.finish or activity.finish here. It will 
                      //finish this activity    
                    //and will take you to the previous activity (in your case 
                    //to mainActivity)

}) If you need any further help feel free to mention it in the comments

CodePudding user response:

Add this in the container dialog

dialog?.setOnKeyListener { dialog, keyCode, event ->
        if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_UP) {
            handleBack() // your code
            return@setOnKeyListener true
        } else
            return@setOnKeyListener false
    }
  • Related