How to prevent Multiple Toast Overlaps

后端 未结 8 1074
难免孤独
难免孤独 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 07:53

    This my solution works perfect both for 4.* and 2.3 Android versions

    static Toast toast;
    .....
    
    if (toast != null)
        toast.cancel();
    
    boolean condition = Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB;
    if ((toast == null && condition) || !condition)
        toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
    if ((toast != null && condition))
        toast.setText(text);
    toast.show();
    

提交回复
热议问题