How to set onDismiss/CancelListener for custom dialog outside the class?

cjacky475 :

I want to finish the activity when custom dialog is cancelled or dismissed. But when I use .setOnDismissListener in other classes, it is never being reached inside. I've found several problems, but the solution was to override onDismiss method inside the customDialog class. But I do not need to override onDismiss method for every customDialog I create. What should I do?

This is the code I call in another class, but never receive message in log "setOnDismissListener".

customDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {                                                       
        Log.d(TAG, "setOnDismissListener");
    }
});

My CustomDialog class:

public class CustomDialog extends Dialog {
    private static final String TAG = "CustomDialog";

    public CustomDialog(Context context, String title, String message) {
        super(context);

        TextView textView = new TextView(context);

        textView.setGravity(Gravity.CENTER);
        textView.setPadding(10, 50, 10, 10);
        textView.setText(title);
        textView.setTextColor(Color.BLACK);
        textView.setTextSize(20);

        Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
        textView.setTypeface(boldTypeface);

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder
                .setCustomTitle(textView)
                .setMessage(message)
                .setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });

        AlertDialog customDialog = builder.show();

        TextView messageText = customDialog.findViewById(android.R.id.message);
        if (messageText != null) {
            messageText.setGravity(Gravity.CENTER);
            messageText.setTextColor(Color.GRAY);
        } else {
            Log.w(TAG, "messageText is null");
        }
    }
}
Gaurav Mall :

Yeah, so if you are not using some API to parse information, or are using local variables I suggest you do whatever functionality you want to do in your onClickListener() Method.

The problem is that you are using your CustomDialog which itself extends the Dialog Class. But instead of using that you create a new alert dialog and build that. You dismiss it, but the dialog which is dismissed is not your custom dialog class, but the builder dialog you created in your constructor. Even if you fixed for that, it introduces unnecessary complications.

What I suggest you do is create the Intent in your onClickListener() function. The way to do that would be to change your constructor to support a callback listener. Simply put you cannot just add an onDismissListener() when the dialog you listen to is another one. What you can do is pass in the function that you want to do when the user dismisses the dialog as a special case. See below.

  1. So, first, modify your constructor like this:

    public CustomDialog(Context context, String title, String message, 
    DialogInterface.OnClickListener listener) {
         super(context);
    }
    
  2. In your constructor paste your previous code:

    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setPadding(10, 50, 10, 10);
    textView.setText(title);
    textView.setTextColor(Color.BLACK);
    textView.setTextSize(20);
    
    Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
    textView.setTypeface(boldTypeface);
    
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder
            .setCustomTitle(textView)
            .setMessage(message)
            .setPositiveButton("Ok", listener);
    
    AlertDialog customDialog = builder.show();
    
    TextView messageText = customDialog.findViewById(android.R.id.message);
    if (messageText != null) {
        messageText.setGravity(Gravity.CENTER);
        messageText.setTextColor(Color.GRAY);
    } else {
        Log.w(TAG, "messageText is null");
    }
    

    What you do is where you used to create a new onClickListener() you pass in the listener parameter.

  3. Go to your MainActivity or where you create your custom dialog. There do this:

    CustomDialog customDialog = new CustomDialog(FirstActivity.this, "Title", "Message", 
    new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
               //Do your functionality here.
               Intent intent = new Intent(context, activity.class);
               //Add any flags if you want
               ...
               context.startActivity(intent);
    
               //Or you can simply do context.finish();
        }
    });
    

    When you don't want to pass a onClickListener()(meaning when you don't want to finish() the activity) pass in null.

It would work. If this is not what you wanted, tell me and I will fix it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=154408&siteId=1