Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to avoid Toast accumulation in Android

Tags:

java

android

In Android, when I create Toast and show them, they appear consecutively. The problem is that I have a button that checks some fields and if the user enters incorrect data, a Toast is shown. If the user touches the button repeatedly, Toasts are accumulated and the message does not disappear for a couple of seconds.

Which is the best way to avoid that?

  • May I save the reference to the last Toast and remove it before making a new one?
  • Should I use the same Toast for all messages?
  • Might I use any method that clears all the Application Toasts before making and showing the new one?
like image 832
Didac Perez Parera Avatar asked Sep 07 '13 18:09

Didac Perez Parera


People also ask

How do I stop toast messages on android?

"Settings" > "Notifications" > under "Recently Sent", click "More" > then at the top, drop the list down to select "All" > and then just turn off the apps you don't want to see anything from at all. BTW, you don't have to wait for the toast to go away on its own.

How do you stop a toast from loading?

You can call cancel() on this object to cancel it, then show the new one. Show activity on this post. If you'd like to cancel ANY Toast (even if the messages are different), you could leave out the whole currentMessage part and include currentToast != null as a check for cancelling.


2 Answers

You can use the cancel() method of Toast to close a showing Toast.

Use a variable to keep a reference to every Toast as you show it, and simply call cancel() before showing another one.

private Toast mToast = null; // <-- keep this in your Activity or even in a custom Application class

//... show one Toast
if (mToast != null) mToast.cancel();
mToast = Toast.makeText(context, text, duration);
mToast.show();

//... show another Toast
if (mToast != null) mToast.cancel();
mToast = Toast.makeText(context, text, duration);
mToast.show();

// and so on.

You could even wrap that into a small class like so:

public class SingleToast {

    private static Toast mToast;

    public static void show(Context context, String text, int duration) {
        if (mToast != null) mToast.cancel();
        mToast = Toast.makeText(context, text, duration);
        mToast.show();
    }
}

and use it in your code like so:

SingleToast.show(this, "Hello World", Toast.LENGTH_LONG);

//

like image 111
Ridcully Avatar answered Sep 23 '22 05:09

Ridcully


Have only one Toast in this activity.

private Toast toast = null;

Then just check if there's currently a Toast being shown before creating another one.

if (toast == null || !toast.getView().isShown()) {
    if (toast != null) {
        toast.cancel();
    }
    toast = Toast.makeToast("Your text", Toast.LENGTH).show();
}

You can even make that last snippet into a private method showToast(text) to refactor code if you need to display different text messages.

like image 32
Juan Andrés Diana Avatar answered Sep 21 '22 05:09

Juan Andrés Diana