Avoiding a toast if one toast is shown

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

But if I execute the action quickly on the SeekBar , then the UI thread is somehow blocked, and the Toast message waits until the UI thread is free.

Now I am worried about avoiding the appearance of a new Toast message if the Toast message is already displayed. Or is there any condition under which we check that the user interface thread is currently free, then I will Toast message.

I tried this in both directions using runOnUIThread() , and also creating a new Handler .

+36
android android-toast
Aug 03 2018-11-11T00:
source share
10 answers

I tried different things to do this. At first I tried to use cancel() , which did not affect me (see also this answer ).

With setDuration(n) I didn't go anywhere either. It turned out that he writes getDuration() , that it carries a value of 0 (if the makeText() parameter was Toast.LENGTH_SHORT ) or 1 (if the makeText() parameter was Toast.LENGTH_LONG ).

Finally, I tried to check if there is isShown() toast isShown() . Of course, this is not so if there is no toast, but even more, it returns a fatal error in this case. Therefore, I needed to try to catch a mistake. Now isShown() returns true if a toast is displayed. Using isShown() , I came up with a 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 } 
+56
Jul 11 2018-12-21T00:
source share

Below is an alternative solution to the most popular answer without try / catch.

 public void showAToast (String message){ if (mToast != null) { mToast.cancel(); } mToast = Toast.makeText(this, message, Toast.LENGTH_SHORT); mToast.show(); } 
+36
Apr 11
source share

A clean solution that works out of the box. Define this in your activity:

 private Toast toast; /** * Use this to prevent multiple Toasts from spamming the UI for a long time. */ public void showToast(CharSequence text, int duration) { if (toast == null) toast = Toast.makeText(this, text, duration); else toast.setText(text); toast.show(); } public void showToast(int resId, int duration) { showToast(getResources().getText(resId), duration); } 
+3
Jun 21 '17 at 15:29
source share

An enhanced function from the aforementioned stream that will show a toast only if not displayed with the same text message:

  public void showSingleToast(){ try{ if(!toast.getView().isShown()) { toast.show(); } } catch (Exception exception) { exception.printStackTrace(); Log.d(TAG,"Toast Exception is "+exception.getLocalizedMessage()); toast = Toast.makeText(this.getActivity(), getContext().getString(R.string.no_search_result_fou'enter code here'nd), Toast.LENGTH_SHORT); toast.show(); } } 
+2
Jul 21 '16 at 8:32
source share

track the last time you showed a toast, and re-show it without-op if it falls within a certain interval.

 public class RepeatSafeToast { private static final int DURATION = 4000; private static final Map<Object, Long> lastShown = new HashMap<Object, Long>(); private static boolean isRecent(Object obj) { Long last = lastShown.get(obj); if (last == null) { return false; } long now = System.currentTimeMillis(); if (last + DURATION < now) { return false; } return true; } public static synchronized void show(Context context, int resId) { if (isRecent(resId)) { return; } Toast.makeText(context, resId, Toast.LENGTH_LONG).show(); lastShown.put(resId, System.currentTimeMillis()); } public static synchronized void show(Context context, String msg) { if (isRecent(msg)) { return; } Toast.makeText(context, msg, Toast.LENGTH_LONG).show(); lastShown.put(msg, System.currentTimeMillis()); } } 

and then

 RepeatSafeToast.show(this, "Hello, toast."); RepeatSafeToast.show(this, "Hello, toast."); // won't be shown RepeatSafeToast.show(this, "Hello, toast."); // won't be shown RepeatSafeToast.show(this, "Hello, toast."); // won't be shown 

this is not ideal since the lengths of LENGTH_SHORT and LENGTH_LONG undefined, but in practice it works well. it takes precedence over other solutions that you don’t need to hold onto the Toast object, and the call syntax remains concise.

+1
Aug 27 2018-12-12T00:
source share

Combined solution

In my case, I needed to cancel the current toast, if it is shown, and display another.

This was supposed to solve the scenario when a user requests a service while it is still loading or unavailable. I need to show a toast (maybe I will be different if the requested service is different). Otherwise, the toasts will be displayed in order, and it will take a lot of time to automatically hide them.

So basically I save the toast instance that I create and the following code: how to safely cancel it

 synchronized public void cancel() { if(toast == null) { Log.d(TAG, "cancel: toast is null (occurs first time only)" ); return; } final View view = toast.getView(); if(view == null){ Log.d(TAG, "cancel: view is null"); return; } if (view.isShown()) { toast.cancel(); }else{ Log.d(TAG, "cancel: view is already dismissed"); } } 

And to use it, now I can not worry about canceling, as in:

 if (toastSingleton != null ) { toastSingleton.cancel(); toastSingleton.showToast(messageText); }else{ Log.e(TAG, "setMessageText: toastSingleton is null"); } 

showToast is up to you how to implement it, since I need a custom look for my toast.

+1
Feb 27 '17 at 10:51 on
source share

Good for stopping styling, for example. Click running toast. Based on @Addi's answer.

 public Toast toast = null; //.... public void favsDisplay(MenuItem item) { if(toast == null) // first time around { Context context = getApplicationContext(); CharSequence text = "Some text..."; int duration = Toast.LENGTH_SHORT; toast = Toast.makeText(context, text, duration); } try { if(toast.getView().isShown() == false) // if false not showing anymore, then show it toast.show(); } catch (Exception e) {} } 
0
Jun 13 '15 at 15:47
source share

Check if the toast message is displayed on the screen, whether it is displayed or not. To display a toast message, make a separate class. And use the method of this class that displays the toast message after checking the visibility of the toast message. Use this piece of code:

 public class AppToast { private static Toast toast; public static void showToast(Context context, String message) { try { if (!toast.getView().isShown()) { toast=Toast.makeText(context, message, Toast.LENGTH_SHORT); toast.show(); } } catch (Exception ex) { toast=Toast.makeText(context,message,Toast.LENGTH_SHORT); toast.show(); } } } 

I hope this solution helps you.

thank

0
Feb 22 '18 at 8:15
source share

Added timer to remove toast after 2 seconds.

 private Toast toast; public void showToast(String text){ try { toast.getView().isShown(); toast.setText(text); }catch (Exception e){ toast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT); } if(toast.getView().isShown()){ new Timer().schedule(new TimerTask() { @Override public void run() { toast.cancel(); } }, 2000); }else{ toast.show(); } } showToast("Please wait"); 
0
Sep 20 '18 at 9:44
source share

My decision:

 public class Utils { public static Toast showToast(Context context, Toast toast, String str) { if (toast != null) toast.cancel(); Toast t = Toast.makeText(context, str, Toast.LENGTH_SHORT); t.show(); return t; } } 

and the caller must have a Toast member for this method parameter, or

 class EasyToast { Toast toast; Context context; public EasyToast(Context context) { this.context = context; } public Toast show(String str) { if (toast != null) toast.cancel(); Toast t = Toast.makeText(context, str, Toast.LENGTH_SHORT); t.show(); return t; } } 

there is a helper class like this.

0
Jun 27 '19 at 4:23
source share



All Articles