How to prevent dialog to open twice

The current way should be to use an DialogFragment. With that, you don't need extra code logic, if you just use your tags and skip loading if that Fragment with tag already exists:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("TAG_DIALOG");
        if (prev == null) {
            ft.addToBackStack(null);
            // Create and show the dialog.
            DialogFragment newFragment = ImageDialog.newInstance(b);
            newFragment.show(ft, "TAG_DIALOG");
        }

make a field for your dialog, like

private Dialog m_dialog = null;

and in your onClick listener check it's status:

if ((m_dialog == null) || !m_dialog.isShowing()){
    m_dialog = new Dialog(...); // initiate it the way you need
    m_dialog.show();
}

edit btw, if you don't need to initialize dialog every time you may separate if() clause like this:

if (m_dialog == null){
    m_dialog = new Dialog(...); // initiate it the way you need
    m_dialog.show();
} 
else if (!m_dialog.isShowing()){
    m_dialog.show();
}

May be this will help you:

Take a count variable i.e., count=0;. In button click validate condition such that if(count==0) show dialog and make count to 1. (with this dialog will not open second time) while dismissing dialog make count to 0 again.

I think this will work

Hope it helps.

Tags:

Android