Stop all toast messages when going to another scre

2020-01-27 07:11发布

enter image description here enter image description here

I am displaying a simple Toast when I click a button. My issue is, when I click on a button multiple times, that Toast message keeps displaying until I get to the main screen. I would like to stop the Toast when I get to the main screen and kill the Toast message in the corresponding activity. I have attached a screenshot.

I have written code as follows:

public class Main extends Activity {

    Dialog d;
    Toast t;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                t = Toast.makeText(Main.this, "you clicked on button!", Toast.LENGTH_LONG);
                t.show();
            }
        });
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        t.cancel();
    }
}

How can I do it?

标签: android toast
3条回答
Animai°情兽
2楼-- · 2020-01-27 07:51

Hi I have the same problem. The problem is that the Toast overlaps e.g. if you press 10 times the Toast will stay 10 x LENGTH_SHORT. The only solution I came with was to control the time the Toast is shown by myself. When you show a Toast just keep a track of the last time you show it, it's still on the screen don't show it again. In your worst case the Toast will be visible only LENGTH_SHORT time.

查看更多
神经病院院长
3楼-- · 2020-01-27 08:09

Toast.makeText returns a Toast object. You can call cancel() on this object to cancel it, then show the new one.

查看更多
▲ chillily
4楼-- · 2020-01-27 08:11

This simple solution works perfectly for me, a singleton which cancels any currently shown toasts:

public enum SingleToast {
    INSTANCE;

    private Toast currentToast;
    private String currentMessage;

    public void show(Context context, String message, int duration) {
        if (message.equals(currentMessage)) {
            currentToast.cancel();
        }
        currentToast = Toast.makeText(context, message, duration);
        currentToast.show();

        currentMessage = message;
    }
}

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.

Drawback of the first method is that when you start 2 different toasts (#1 & #2), you won't be able to cancel them anymore when you start #1: this happens because the singleton saved #2 as the 'current' even though it is not shown yet. So when you start #1 again it is still added to the queue of 2 Toasts: #1, #2, #1, and all will be shown.

So choose whatever of these 2 solutions fits your usecase, or extend this simple singleton with timers, guess that's what Mojo suggested.

Calling the toast is simple: I use it to show the message of some custom exceptions:

public abstract class CustomToastException extends Exception {
    public CustomToastException(String message) {
        super(message);
        SingleToast.INSTANCE.show(context, getMessage(), Toast.LENGTH_LONG);
    }
}

Note, in case anyone was wondering: I've raised a new question whether my solution does not cause any trouble: Static context saved in Application class and used in a singleton toast builder, does this create a memory leak?

查看更多
登录 后发表回答