topxebec topxebec - 3 months ago 34
Android Question

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

In my app,I construct a calendar widget for my actvity, 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 a invisible queue to manage toasts

how can I clean it and only show the last toast?Can I show a specificate Toast immediately without waiting?

I searched the "android.widget.Toast.java" and find a method "cancel()",but unfortunately it dose not work as follow.

if (t != null) {
t.cancel();
}
t = Toast.makeText(this.mContext, mHelper.getYear() + "年"
+ (mHelper.getMonth() + 1) + "月", Toast.LENGTH_SHORT);
t.show();

Answer

You need to call method on correct object.

toastObject.cancel()