How to prevent dialog to open twice
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 dial开发者_运维问答og twice.
So can anyone suggest me how can I prevent this dialog to open twice on doubleClick of my button?
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.
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");
}
When the Button
is pressed, disable it using Button.setEnabled(false)
. When the dialog finishes, re-enable the Button
using a DialogInterface.OnDismissListener
. This way you don't have to hold a global reference to your dialog.
You should create dialog variable before onClick. For example:
private fun onFilterClick() {
val dialog = FilterDialogFragment()
btn_filter.setOnClickListener {
if (!dialog.isAdded)
dialog.show(supportFragmentManager, dialog.tag)
}
}
I was facing the same problem for last 2 days and after a lots of hit and trial I found a small problem in my code that I was not dismissing the dialog which I opened in onPostExecute() of my AsyncTask to do some extra work after AsyncTask Work.
When I called myCustomDialog.dismiss()
everything worked fine.
boolean isBusy = false;
View.OnClickListener ShowDialog_Click(){
return new View.OnClickListener() {
@Override
public void onClick(View view) {
if(isBusy){
return;
}
isBusy = true;
//show dialog
}
};
}
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
dialog.dismiss();
isBusy = false;
}
});
精彩评论