Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent dialog to open twice

Tags:

android

I had button in my app, on doubleClick of my button I open a dialog. Sometimes when I doubleClick the button in a fast way then the dialog opens twice, as a result of which the user has to cancel the dialog twice.

So can anyone suggest me how can I prevent this dialog to open twice on doubleClick of my button?

like image 949
AndroidDev Avatar asked Sep 23 '11 10:09

AndroidDev


2 Answers

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();
}
like image 88
Vladimir Avatar answered Sep 20 '22 13:09

Vladimir


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.

like image 26
Udaykiran Avatar answered Sep 22 '22 13:09

Udaykiran