2

I'm using Toast to show some informations to the user, because I want to show the newest message without delay regardless of the previous messages, I do it like this (learned from old projects):

public class MainActivity extends Activity {

    private Toast mToast;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
    }

    private void toast(final String message) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mToast.setText(message);
                mToast.show();
            }
        });
    }
}

That is, the single Toast object is reused and showed multiple times, whenever I need show a new message, I just setText and show it again. It seems working fine, but after I did some searching on Google, I found most people will do it like this:

    private void toast(final String message) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mToast.cancel();
                mToast = Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT);
                mToast.show();
            }
        });
    }

Which cancel the previous Toast then make a new one by Toast.makeText.

Are there any differences? Which one should I prefer?

Hong Duan
  • 4,234
  • 2
  • 27
  • 50
  • 5
    In general its better to cancel a toast before you show a new one. the reason is if you are showing multiple toasts in a short period of time, it can be confusing for the user if they are seeing a toast that has nothing to do with the action they just performed – Eoin Sep 28 '15 at 04:30
  • Yes, but the two methods are just identical to users. – Hong Duan Sep 28 '15 at 04:36
  • Yes, but if you need to show a toast immediately, its better to cancel the toast. Its more of a problem if you are using a LENGTH_LONG toast – Eoin Sep 28 '15 at 04:42
  • Possible duplicate of [How to prevent Multiple Toast Overlaps](http://stackoverflow.com/questions/12922516/how-to-prevent-multiple-toast-overlaps) – Ciro Santilli OurBigBook.com Feb 02 '16 at 16:26
  • Which one you should prefer definitely only depends on what you want. Do you want the toasts stacked or not? – NoDataDumpNoContribution Feb 02 '16 at 17:02

2 Answers2

1

You can cache current Toast in Activity's variable, and then cancel it just before showing 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 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();
}

Reference : How to immediately replace the current toast with a second one without waiting for the current one to finish?

Community
  • 1
  • 1
KishuDroid
  • 5,411
  • 4
  • 30
  • 47
0

You should try code something like this to avoid visibility of multiple toast at a time.

 private void toast(final String message) {
 try{ mToast.getView().isShown();     // true if visible
        mToast.setText(message);
    } catch (Exception e) {         // invisible if exception
        mToast = Toast.makeText(theContext, message, toastDuration);
        }
    mToast.show();  //finally display it
}

code help me is here

Community
  • 1
  • 1
pRaNaY
  • 24,642
  • 24
  • 96
  • 146