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

后端 未结 11 1398
粉色の甜心
粉色の甜心 2020-12-05 17:18

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 n

11条回答
  •  萌比男神i
    2020-12-05 17:58

    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

    private Toast mToast;
    

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

    if(mToast != null) {
      mToast.cancel();  //if a toast exists it deletes it, allowing you to create a new one
    }
    
    
    mToast = Toast.makeText(this, "This will show up now!", Toast.LENGTH_LONG);
    mToast.show(); //creates the new toast. 
    

提交回复
热议问题