How to prevent Multiple Toast Overlaps

后端 未结 8 998
难免孤独
难免孤独 2020-11-29 07:26

I\'ve been using a common \"myToast\" which I use \"myToast.cancel() prior to issuing a new toast. For Android v2.3 and older, this works great. When a new toas

相关标签:
8条回答
  • 2020-11-29 08:05

    Did nandeesh's solution not work for you? His solution would be cleaner than using two different toasts.

    For example, (expanding on his/her answer) prior to onCreate we'd declare the toast:

    private Toast myToast;
    

    and in onCreate we'd have to initialize it using makeToast (otherwise we'd get an error):

    myToast = Toast.makeText(getApplicationContext(), null, Toast.LENGTH_SHORT);
    

    and whenever we want a toast to be shown we'd simply call:

    myToast.setText("some text");
    myToast.show();
    

    and this would replace the previous toast properly.

    0 讨论(0)
  • 2020-11-29 08:08

    cancel() doesn't do anything I'm afraid.

    I would suggest using Crouton https://github.com/keyboardsurfer/Crouton

    0 讨论(0)
提交回复
热议问题