How to prevent Multiple Toast Overlaps

后端 未结 8 1075
难免孤独
难免孤独 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.

提交回复
热议问题