Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel a toast on Android before it appears [duplicate]

Tags:

android

toast

My application displays a Toast when a certain action happens. If two of these actions happen in close proximity, however, I would like to forgo displaying the first Toast, instead displaying only the second one. I thought Toast.cancel() would do the trick, but what it does is simply hide the first toast; the second one only displays after the first one would have finished displaying anyway.

Example code:

Toast toast1 = Toast.makeText(parentActivity, "Test1", Toast.LENGTH_SHORT);
Toast toast2 = Toast.makeText(parentActivity, "Test2", Toast.LENGTH_SHORT);

toast1.show();
toast2.show();

toast1.cancel();

The second Toast shows up only after waiting a short while (the length of the short duration). This in fact happens even if I call toast2.cancel().

like image 383
Joey Marianer Avatar asked Feb 01 '11 19:02

Joey Marianer


2 Answers

I'm not sure this would work, but maybe try cancelling both of them and then showing the second one again.

like image 186
Liquid_Fire Avatar answered Oct 11 '22 11:10

Liquid_Fire


Toast.makeText(context, text, duration) returns a Toast object. Call cancel() method on this object to cancel it.

Example:

Toast mToastText = Toast.makeText(getApplicationContext(), "Hello StackOverFlow!", Toast.LENGTH_SHORT);
 mToastText.cancel();
like image 36
Jorgesys Avatar answered Oct 11 '22 10:10

Jorgesys