tonix
tonix

Reputation: 6959

How to trigger an event on Dialog dismiss Android?

I have a custom dialog which extends the Dialog Class, I would like to bind an event to execute some code after the Dialog is closed when the user presses the BACK button of the device. How can I do that? I found a post where someone says that the .addWindowListener() should be used when working with Dialogs and other Window widgets. But the dialog class doesn't have the addWindowListener method, so I cannot use it. Is there another way without using fragments cause I shouldn't re-write the MyCustomDialog class?

This is the code:

public class MyCustomDialog extends Dialog {

public MyCustomDialog(Context context, int layoutResourceId) {
    super(context);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(layoutResourceId);

}
}  

Thanks for the attention!

EDIT: i found this on the android developers site, is there a way to use it with MyCustomDialog class?

onDismiss DialogInterface

Upvotes: 4

Views: 5576

Answers (4)

Paulo Buchsbaum
Paulo Buchsbaum

Reputation: 2659

The way I adopted in Kotlin (Java is similar) is insert the below code in all the dialogs that are created.

This listener will be triggered if the user taps the outside of the dialog unless dial.setCanceledOnTouchOutside(false) and also with Back option touched (at the bottom of the Android phone screen) unless dial.setcancelable(false)

val dial = Dialog(.....)
...
dial.setOnCancelListener {  // insert
 // your code               // insert 
}                           // insert
dial.show()

Where dial points to each dialog that is created on your system.

In you code you put garbage collection and whatever code you want.

Upvotes: 0

Niranjan Girhe
Niranjan Girhe

Reputation: 21

If you want to trigger an event if user clicked/touched outside the dialog and close it or used the back button to close then use

dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                //your trigger goes here
                Toast.makeText(IntroductoryActivity.this, "on cancel", Toast.LENGTH_SHORT).show();
            }
        });

But if you want to trigger an event if something dismisses the dialog, like some other event then use

dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                //your trigger goes here
                Toast.makeText(IntroductoryActivity.this, "on dismiss", Toast.LENGTH_SHORT).show();
            }
        }); 

Upvotes: 0

TheMohanAhuja
TheMohanAhuja

Reputation: 1875

Since you are extending android Dialog class you can implement a Dismiss Listener in your Activity's and set it when you create the Dialog, and then in the listener implement any functionality you want depending on the button that was used to dismiss the Dialog.

Hope this will solve your problem.

Edit You can use dialog.setCanceledOnTouchOutside(false); which will stop closing the dialog if you touch outside of the dialog.

Something like,

  Dialog dialog = new Dialog(context)
  dialog.setCanceledOnTouchOutside(false);

OR Alternatively

Override onTouchEvent() of dialog and check for action type. if the action type is 'MotionEvent.ACTION_OUTSIDE' means, user is interacting outside the dialog region. So in this case, you can dimiss your dialog or decide what you wanted to perform. view plainprint?

dialog.setOnTouchListener(new View.OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event)  
    {  

       if(event.getAction() == MotionEvent.ACTION_OUTSIDE){  
            Toast.make(getApplicationContext(), "TOuched outside the dialog", Toast.LENGTH_LONG).show();  
               this.dismiss();  
       }  
       return false;  
    }
});  

And for back press you can do dialog.setCancelable(false); which will prevent dialog getting cancelled from backpress event.

OR you can alternatively override setOnKeyListener event and put your own code into it.

Edit

dialog.setOnKeyListener(new Dialog.OnKeyListener() {

        @Override
        public boolean onKey(DialogInterface arg0, int keyCode,
                KeyEvent event) {
            // TODO Auto-generated method stub
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                finish();
                dialog.dismiss();
            }
            return true;
        }
    });

Happy Coding!!!

Upvotes: 3

Chirag Ghori
Chirag Ghori

Reputation: 4231

You need to Override onBackPressed inside Dialog class. Also make sure to close dialog after override OnBackPressed .

Try this

public class MyCustomDialog extends Dialog {

public MyCustomDialog(Context context, int layoutResourceId) {
    super(context);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(layoutResourceId);

}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

     dismiss();   // make sure to call dismiss to close dialog

    // put your code here
}
}

Upvotes: 1

Related Questions