How to prevent Multiple Toast Overlaps

后端 未结 8 1090
难免孤独
难免孤独 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:00

    Make a java class as ShowToast.java like below

        public class ShowToast {
    
            private static Toast toast;
    
            public static void show(Context mcontext, String text) {
                if (toast != null) 
                   toast.cancel();
                toast = Toast.makeText(mcontext, text, Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    

    Then call it as

        ShowToast.show(getApplicationContext(),"YOUR_TOAST_TEXT");
    

提交回复
热议问题