Search code examples
androidtoast

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


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.cancel();
}
t = Toast.makeText(this.mContext, mHelper.getYear() + "年"
                + (mHelper.getMonth() + 1) + "月", Toast.LENGTH_SHORT);
t.show();

Solution

  • You need to call method on correct object.

    toastObject.cancel()