The trick is to keep track of the last Toast that has been shown and undo it.
What I did was create a Toast wrapper containing a static link to the last displayed toast.
When I need to show a new one, I first canceled the static link before showing the new one (and saved it in the static).
Here's the full Boast wrapper code I created - it mimics Toast methods so I can use them. By default, Boast will override the previous one, so you will not create a queue of pending Toasts.
This code can be found in my Github gist:
If you just want to learn how to cancel notifications when you exit the application, you will find a lot of help there. If you have any improvements or suggestions, please feel free to unblock them and contact us. This is a very old answer, but the code has been stable in production on several applications for some time.
BTW - In most cases, this should be a direct replacement for Toast .
package mobi.glowworm.lib.ui.widget; import android.annotation.SuppressLint; import android.content.Context; import android.content.res.Resources; import android.support.annotation.Nullable; import android.widget.Toast; import java.lang.ref.WeakReference; /** * {@link Toast} decorator allowing for easy cancellation of notifications. Use this class if you * want subsequent Toast notifications to overwrite current ones. </p> * <p/> * By default, a current {@link Boast} notification will be cancelled by a subsequent notification. * This default behaviour can be changed by calling certain methods like {@link
Richard Le Mesurier Apr 19 '13 at 7:57 2013-04-19 07:57
source share