How to immediately replace the current toast with a second, without waiting for the end of the current?

I have a lot of buttons. And at the click of each of them I show a toast. But while the toast is loading and displayed in the field of view, another button is pressed, and the toast is not displayed until the one that is displayed ends.

So, I would like to understand the detection method if a toast is displayed in the current context. Is there a way to find out if a toast is displayed so that I can cancel it and display a new one.

+7
source share
1 answer

You can cache the current Toast in the Activity variable, and then cancel it before showing the next toast. Here is an example:

 Toast m_currentToast; void showToast(String text) { if(m_currentToast != null) { m_currentToast.cancel(); } m_currentToast = Toast.makeText(this, text, Toast.LENGTH_LONG); m_currentToast.show(); } 

Another way to instantly update a Toast message:

 void showToast(String text) { if(m_currentToast == null) { m_currentToast = Toast.makeText(this, text, Toast.LENGTH_LONG); } m_currentToast.setText(text); m_currentToast.setDuration(Toast.LENGTH_LONG); m_currentToast.show(); } 
+26
source

All Articles