Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use onDismiss() when using custom dialogs - Android

I'm working on a little program, and I need to add a custom dialog that passes some info to the calling acitivity when it closes. I extended the dialog class, and when I try to capture the custom dialog when it closes,using an onDismiss listener, it never reaches it because I used a custom dialog.

This is part of my activity -

    .
    .
    .
       attributes customizeDialog = new attributes(con,position,pick.getLastVisiblePosition());
        customizeDialog.show();

(The attributes being the name of the class that extends the dialog class).

Here is the event listener I set up when the dialog finishes -

    customizeDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialog) {
            Log.v("LOG_CAT",attributes.selectedIndexes.get(0) + " " + attributes.selectedIndexes.get(1) + " " + attributes.selectedIndexes.get(2) + " " + attributes.selectedIndexes.get(3) + " " + attributes.selectedIndexes.get(5) + " ");
    }

});

I know i'm doing it wrong,I just don't know how to fix it.

I would really appreciate any help with this problem.

Thanks!

like image 389
Tofira Avatar asked Feb 06 '11 22:02

Tofira


4 Answers

I tend to have my activity implement listeners like this...

public class MyActivity extends Activity
    implements DialogInterface.OnDismissListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        attributes customizeDialog = new attributes(con,position,pick.getLastVisiblePosition());
        customizeDialog.setOnDismissListener(this);
        customizeDialog.show();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        // Do whatever
    }
}
like image 114
Squonk Avatar answered Sep 20 '22 02:09

Squonk


You could have your calling activity implement a custom listener interface that is called when the dialog closes:

public interface MyDialogListener {
    void OnCloseDialog();
}

public class MyActivity implements MyDialogListener {
    public void SomeMethod() {
        MyDialog myDialog = new MyDialog(this, this);
        myDialog.show();
    }

    public void OnCloseDialog() {
        // Do whatever you want to do on close here
    }

}

public class MyDialog extends Dialog {
    MyDialogListener mListener;

    public MyDialog (Context context, MyDialogListener listener) {
        super(context, R.style.Dialog);
        mListener = listener;
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.CloseButton:
                mListener.OnCloseDialog();
                dismiss()
                break;
            default:
                //...
        }
    }
}

This is especially useful if you want to send stuff back to the caller at any other time besides on dismissal.

like image 35
TREVOR Avatar answered Sep 20 '22 02:09

TREVOR


And if you want to have some sort of saving inside the dialog, again, you have to use onDicmissListener since for custom dialogs onDismiss is not called by default:

public class CustomDialog extends Dialog implements DialogInterface.OnDismissListener {

    public CustomDialog(Context context) {
        super(context);
        setupLayout(context);
    }

    public CustomDialog(Context context, int theme) {
        super(context, theme);
        setupLayout(context);
    }

    protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        setupLayout(context);
    }

    private void setupLayout(Context context) {
        this.context = context;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_dialog);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.FILL_PARENT;
        getWindow().setAttributes(params);

        setOnDismissListener(this);

        loadPreferences();
    }

    private void loadPreferences() {
      // ...
    }

    private void savePreferences() {
       // ...
    }

    @Override
    public void onDismiss(DialogInterface dialogInterface) {
        savePreferences();
    }
}
like image 1
Mohsen Afshin Avatar answered Sep 20 '22 02:09

Mohsen Afshin


If you are using custom dialog and can't dismiss it, try below code. It worked for me.

 new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
           dialog.dismiss();
        }
    }, 1500);
like image 1
Priyanka Thakkar Avatar answered Sep 20 '22 02:09

Priyanka Thakkar