Stop the toast and run another in android

I am using Toast in my application. When I press the button, it shows a Toast. My problem is that the second time I press the button, the second toast "waits" for the first to end, and only what it shows. I want the current one to be displayed immediately and not wait. This is my simple code:

toast = Toast.makeText(getApplicationContext(), "Press Back to retorn to the main page", Toast.LENGTH_SHORT);
toast.show();

How can i do this?

+5
source share
4 answers

At fooobar.com/questions/115938 / ... the writer did not cancel the toast, they just changed its text.

+3
source

You can always cancel the Toast object .

final Toast tst = Toast.makeText(ctx, "This is a toast.", Toast.LENGTH_SHORT);
tst.show();

Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
       @Override
       public void run() {
           tst.cancel(); 
           tst.setText("Same toast with another message.");
           tst.show();
       }
}, 1000);

, Toast , , .

+5

Cancel the original Toast, set a new message and show the message again Toast.

Toast mytoast;
mytoast = Toast.makeText(this, "Hi Ho Jorgesys! ", Toast.LENGTH_LONG);
mytoast.show();
....
....
....
if(CancelToast){
  mytoast.cancel();  //cancelling old Toast!
  mytoast = Toast.makeText(this, "Same toast with another message.", Toast.LENGTH_LONG); //Setting a new message.
  mytoast.show(); //Show the new message!.
}
+2
source

You can use toast.cancel () to display the next toast.

+1
source

All Articles