Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid a Toast if there's one Toast already being shown

I have several SeekBar and onSeekBarProgressStop(), I want to show a Toast message.

But if on SeekBar I perform the action rapidly then UI thread somehow blocks and Toast message waits till UI thread is free.

Now my concern is to avoid the new Toast message if the Toast message is already displaying. Or is their any condition by which we check that UI thread is currently free then I'll show the Toast message.

I tried it in both way, by using runOnUIThread() and also creating new Handler.

like image 652
Deepak Goel Avatar asked Aug 03 '11 10:08

Deepak Goel


People also ask

How do you dismiss a toast?

The toast can be dismissed automatically after a specific amount of time by passing the number of milliseconds to display it in the duration of the toast options. If a button with a role of "cancel" is added, then that button will dismiss the toast.

How do you stop a toast from loading?

You can call cancel() on this object to cancel it, then show the new one. Show activity on this post. If you'd like to cancel ANY Toast (even if the messages are different), you could leave out the whole currentMessage part and include currentToast != null as a check for cancelling.

How long does toast notification last?

Toast messages are informative, have a lifespan of just a few seconds and take up a very small portion of the screen. Toast messages ensure that the use of the application is not interrupted while providing necessary information for the user.


1 Answers

I've tried a variety of things to do this. At first I tried using the cancel(), which had no effect for me (see also this answer).

With setDuration(n) I wasn't coming to anywhere either. It turned out by logging getDuration() that it carries a value of 0 (if makeText()'s parameter was Toast.LENGTH_SHORT) or 1 (if makeText()'s parameter was Toast.LENGTH_LONG).

Finally I tried to check if the toast's view isShown(). Of course it isn't if no toast is shown, but even more, it returns a fatal error in this case. So I needed to try and catch the error. Now, isShown() returns true if a toast is displayed. Utilizing isShown() I came up with the method:

    /**      * <strong>public void showAToast (String st)</strong></br>      * this little method displays a toast on the screen.</br>      * it checks if a toast is currently visible</br>      * if so </br>      * ... it "sets" the new text</br>      * else</br>      * ... it "makes" the new text</br>      * and "shows" either or        * @param st the string to be toasted      */      public void showAToast (String st){ //"Toast toast" is declared in the class         try{ toast.getView().isShown();     // true if visible             toast.setText(st);         } catch (Exception e) {         // invisible if exception             toast = Toast.makeText(theContext, st, toastDuration);             }         toast.show();  //finally display it     } 
like image 129
Addi Avatar answered Oct 07 '22 07:10

Addi