How to prevent Multiple Toast Overlaps

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

    Instead of calling cancel(). Try resetting the text and call show(). This should cancel the last toast by itself

    myToast.setText("wrong key")
    myToast.show();
    

    If you keep using the same myToast instead of creating one every time I guess they won't stack up.

    0 讨论(0)
  • 2020-11-29 07:52

    Create an Toast Object:

    Toast toastobject=null;
    

    Now use the below code to display the toast. This will work find for me

        int index = clickCounter-1;
    
    
        if(toastobject!= null)
                {
                    toastobject.cancel();
                }
                toastobject = Toast.makeText(this,"Toast Text" , Toast.LENGTH_SHORT);
                listItems.remove(index);
                toastobject.show();
    
    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2020-11-29 07:59

    Here is my answer copied from another similar question here:

    • Android cancel Toast when exiting the app and when toast is being shown

    The Boast class accomplishes exactly what you need.


    The trick is to keep track of the last Toast that was shown, and to cancel that one.

    What I have done is to create a Toast wrapper, that contains a static reference to the last Toast displayed.

    When I need to show a new one, I first cancel the static reference, before showing the new one (and saving it in the static).

    Here's full code of the Boast wrapper I made - it mimics enough of the Toast methods for me to use it. By default the Boast will cancel the previous one, so you don't build up a queue of Toasts waiting to be displayed.

    If you just want to know how to cancel the notifications when exiting your app, you will find lots of help in there.


    package mobi.glowworm.lib.ui.widget;
    
    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.content.res.Resources;
    import android.support.annotation.Nullable;
    import android.widget.Toast;
    
    import java.lang.ref.WeakReference;
    
    /**
     * {@link Toast} decorator allowing for easy cancellation of notifications. Use this class if you
     * want subsequent Toast notifications to overwrite current ones. </p>
     * <p/>
     * By default, a current {@link Boast} notification will be cancelled by a subsequent notification.
     * This default behaviour can be changed by calling certain methods like {@link #show(boolean)}.
     */
    public class Boast {
        /**
         * Keeps track of certain Boast notifications that may need to be cancelled. This functionality
         * is only offered by some of the methods in this class.
         * <p>
         * Uses a {@link WeakReference} to avoid leaking the activity context used to show the original {@link Toast}.
         */
        @Nullable
        private volatile static WeakReference<Boast> weakBoast = null;
    
        @Nullable
        private static Boast getGlobalBoast() {
            if (weakBoast == null) {
                return null;
            }
    
            return weakBoast.get();
        }
    
        private static void setGlobalBoast(@Nullable Boast globalBoast) {
            Boast.weakBoast = new WeakReference<>(globalBoast);
        }
    
    
        // ////////////////////////////////////////////////////////////////////////////////////////////////////////
    
        /**
         * Internal reference to the {@link Toast} object that will be displayed.
         */
        private Toast internalToast;
    
        // ////////////////////////////////////////////////////////////////////////////////////////////////////////
    
        /**
         * Private constructor creates a new {@link Boast} from a given {@link Toast}.
         *
         * @throws NullPointerException if the parameter is <code>null</code>.
         */
        private Boast(Toast toast) {
            // null check
            if (toast == null) {
                throw new NullPointerException("Boast.Boast(Toast) requires a non-null parameter.");
            }
    
            internalToast = toast;
        }
    
        // ////////////////////////////////////////////////////////////////////////////////////////////////////////
    
        /**
         * Make a standard {@link Boast} that just contains a text view.
         *
         * @param context  The context to use. Usually your {@link android.app.Application} or
         *                 {@link android.app.Activity} object.
         * @param text     The text to show. Can be formatted text.
         * @param duration How long to display the message. Either {@link Toast#LENGTH_SHORT} or
         *                 {@link Toast#LENGTH_LONG}
         */
        @SuppressLint("ShowToast")
        public static Boast makeText(Context context, CharSequence text, int duration) {
            return new Boast(Toast.makeText(context, text, duration));
        }
    
        /**
         * Make a standard {@link Boast} that just contains a text view with the text from a resource.
         *
         * @param context  The context to use. Usually your {@link android.app.Application} or
         *                 {@link android.app.Activity} object.
         * @param resId    The resource id of the string resource to use. Can be formatted text.
         * @param duration How long to display the message. Either {@link Toast#LENGTH_SHORT} or
         *                 {@link Toast#LENGTH_LONG}
         * @throws Resources.NotFoundException if the resource can't be found.
         */
        @SuppressLint("ShowToast")
        public static Boast makeText(Context context, int resId, int duration)
                throws Resources.NotFoundException {
            return new Boast(Toast.makeText(context, resId, duration));
        }
    
        /**
         * Make a standard {@link Boast} that just contains a text view. Duration defaults to
         * {@link Toast#LENGTH_SHORT}.
         *
         * @param context The context to use. Usually your {@link android.app.Application} or
         *                {@link android.app.Activity} object.
         * @param text    The text to show. Can be formatted text.
         */
        @SuppressLint("ShowToast")
        public static Boast makeText(Context context, CharSequence text) {
            return new Boast(Toast.makeText(context, text, Toast.LENGTH_SHORT));
        }
    
        /**
         * Make a standard {@link Boast} that just contains a text view with the text from a resource.
         * Duration defaults to {@link Toast#LENGTH_SHORT}.
         *
         * @param context The context to use. Usually your {@link android.app.Application} or
         *                {@link android.app.Activity} object.
         * @param resId   The resource id of the string resource to use. Can be formatted text.
         * @throws Resources.NotFoundException if the resource can't be found.
         */
        @SuppressLint("ShowToast")
        public static Boast makeText(Context context, int resId) throws Resources.NotFoundException {
            return new Boast(Toast.makeText(context, resId, Toast.LENGTH_SHORT));
        }
    
        // ////////////////////////////////////////////////////////////////////////////////////////////////////////
    
        /**
         * Show a standard {@link Boast} that just contains a text view.
         *
         * @param context  The context to use. Usually your {@link android.app.Application} or
         *                 {@link android.app.Activity} object.
         * @param text     The text to show. Can be formatted text.
         * @param duration How long to display the message. Either {@link Toast#LENGTH_SHORT} or
         *                 {@link Toast#LENGTH_LONG}
         */
        public static void showText(Context context, CharSequence text, int duration) {
            Boast.makeText(context, text, duration).show();
        }
    
        /**
         * Show a standard {@link Boast} that just contains a text view with the text from a resource.
         *
         * @param context  The context to use. Usually your {@link android.app.Application} or
         *                 {@link android.app.Activity} object.
         * @param resId    The resource id of the string resource to use. Can be formatted text.
         * @param duration How long to display the message. Either {@link Toast#LENGTH_SHORT} or
         *                 {@link Toast#LENGTH_LONG}
         * @throws Resources.NotFoundException if the resource can't be found.
         */
        public static void showText(Context context, int resId, int duration)
                throws Resources.NotFoundException {
            Boast.makeText(context, resId, duration).show();
        }
    
        /**
         * Show a standard {@link Boast} that just contains a text view. Duration defaults to
         * {@link Toast#LENGTH_SHORT}.
         *
         * @param context The context to use. Usually your {@link android.app.Application} or
         *                {@link android.app.Activity} object.
         * @param text    The text to show. Can be formatted text.
         */
        public static void showText(Context context, CharSequence text) {
            Boast.makeText(context, text, Toast.LENGTH_SHORT).show();
        }
    
        /**
         * Show a standard {@link Boast} that just contains a text view with the text from a resource.
         * Duration defaults to {@link Toast#LENGTH_SHORT}.
         *
         * @param context The context to use. Usually your {@link android.app.Application} or
         *                {@link android.app.Activity} object.
         * @param resId   The resource id of the string resource to use. Can be formatted text.
         * @throws Resources.NotFoundException if the resource can't be found.
         */
        public static void showText(Context context, int resId) throws Resources.NotFoundException {
            Boast.makeText(context, resId, Toast.LENGTH_SHORT).show();
        }
    
        // ////////////////////////////////////////////////////////////////////////////////////////////////////////
    
        /**
         * Close the view if it's showing, or don't show it if it isn't showing yet. You do not normally
         * have to call this. Normally view will disappear on its own after the appropriate duration.
         */
        public void cancel() {
            internalToast.cancel();
        }
    
        /**
         * Show the view for the specified duration. By default, this method cancels any current
         * notification to immediately display the new one. For conventional {@link Toast#show()}
         * queueing behaviour, use method {@link #show(boolean)}.
         *
         * @see #show(boolean)
         */
        public void show() {
            show(true);
        }
    
        /**
         * Show the view for the specified duration. This method can be used to cancel the current
         * notification, or to queue up notifications.
         *
         * @param cancelCurrent <code>true</code> to cancel any current notification and replace it with this new
         *                      one
         * @see #show()
         */
        public void show(boolean cancelCurrent) {
            // cancel current
            if (cancelCurrent) {
                final Boast cachedGlobalBoast = getGlobalBoast();
                if ((cachedGlobalBoast != null)) {
                    cachedGlobalBoast.cancel();
                }
            }
    
            // save an instance of this current notification
            setGlobalBoast(this);
    
            internalToast.show();
        }
    
    }
    
    0 讨论(0)
  • 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");
    
    0 讨论(0)
  • 2020-11-29 08:01

    create new function and call this.

    ImageButton ABtn = (ImageButton) findViewById(R.id.Btn);
    ABtn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v)
    {       
    SETToast("mytext");
    }
    });
    
        private Toast toast = null;
    
    public void SETToast( String text)
    {
      if(toast==null)
      {
         toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT); 
         toast.show();
         final Handler handler = new Handler(); 
         handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                toast=null;
            }
         }, 2000);
      }
      else
      {
          toast.setText(text);
      }   
    }
    
    0 讨论(0)
提交回复
热议问题