Dinesh Ravi
Dinesh Ravi

Reputation: 1225

How to prevent multiple Dialog instance being created so that I can cleanly dismiss()?

I have the Bluetooth callback that is sometimes triggering twice and makes handling the dialog instance difficult to dismiss().

I am declaring the Loader instance in Global

LoaderProgress mLConnectdialogLoader = new LoaderProgress(InsoleConnection.this);

I trigger the dialog called "Connecting.." for 5 seconds and then dismiss.

new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                    mLConnectdialogLoader.dismiss()
                    }
  },5000);
  mLConnectdialogLoader.show("Connecting...")

How to prevent the same instance being called twice so that I can avoid having the hard time in dismissing the dialog.

Upvotes: 2

Views: 2177

Answers (3)

S. Alawadi
S. Alawadi

Reputation: 144

if i got the question ,, you can put the "dialog creation coode" in a synchronized method Learn More About it http://tutorials.jenkov.com/java-concurrency/synchronized.html

Upvotes: 1

Nouran S. Ahmad
Nouran S. Ahmad

Reputation: 503

just create a bool and check its status:

boolean isShown=false;   

 new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                        mLConnectdialogLoader.dismiss();
                        isShown=false;
                        }
      },5000);

    if(!isShown){
    mLConnectdialogLoader.show("Connecting...");
    isShown= true;
    }

Upvotes: 1

Eliran Kuta
Eliran Kuta

Reputation: 4358

if(!mLConnectdialogLoader.isShowing())
      mLConnectdialogLoader.show("Connecting...")

In your DialogLoader class:

public boolean isShowing() { return dialog.isShowing(); }

Upvotes: 1

Related Questions