Home » Android » android – Can I cancel previous Toast when I want to show an other Toast?

android – Can I cancel previous Toast when I want to show an other Toast?

Posted by: admin April 23, 2020 Leave a comment


In my app, I construct a calendar widget for my activity, when I scroll it to previous or next month, I let it make a toast and show it.

The question is, the toast need time to show, when I scroll it fast enough, for example, I scrolled to “2012/05” and “2012/06” and scroll to “2012/07” without pause, I have to wait the Toast of “2012/05”, “2012/06″,”2012/07” to show one by one slowly.

Seems like Android has an invisible queue to manage toasts

how can I clean it and only show the last toast? Can I show a specific Toast immediately without waiting?

I searched the “android.widget.Toast.java” and find a method cancel(), but unfortunately it does not work as follows.

if (t != null) {
t = Toast.makeText(this.mContext, mHelper.getYear() + "年"
                + (mHelper.getMonth() + 1) + "月", Toast.LENGTH_SHORT);
How to&Answers:

You need to call method on correct object.



Here is my answer copied from another similar question here:

The Boast class accomplishes exactly what you need. Most recent code can be found on GitHub here:

The trick is to keep track of the last Toast that was shown, and to cancel that one.

What I have done is to create a Toast wrapper, that contains a static reference to the last Toast displayed.

When I need to show a new one, I first cancel the static reference, before showing the new one (and saving it in the static).

Here’s full code of the Boast wrapper I made – it mimics enough of the Toast methods for me to use it. By default the Boast will cancel the previous one, so you don’t build up a queue of Toasts waiting to be displayed.

If you just want to know how to cancel the notifications when exiting your app, you will find lots of help in there.


You just need to declare a “Toast” var like this:

Then in your function, do it like this:


Here is the Code.

Now you can use the Object of toastobject. Its Reference

You can use it in Thread or whenever you would like to Close the Toast.


You can reuse a toast, this will make it display immediately.


There are many ways by which we can cancel previous Toast when we want to show another Toast. below I have written a simplest an easy way to implement it. First of all, we have to create a variable which can be accessed in the whole class.

After creating the variable which can be accessed by whole class we have to create a method in our class which displays the toast message and checks if the previous toast is displaying then cancel it.

you can change toast message by runtime calling above method.

//after some time

hope it helps.


Toast has a method to hide current toast message

Try calling t.cancel() when it is necessary.


You can create static method and use it to show a toast:


Simple. Simply call the method .cancel() on the toast once you want to create another toast.

Start by defining a Toast variable at the top of your class like this

Later, When you want to create a new Toast(and have the old one disappear),
do this.



You can use one shoot technique. Oke let’s start defining:

Later when you wanna show toast once: