简体   繁体   中英

Show toast only if it's different from the existing toast

So my goal is to only have a toast message shown to the user if there is no toast message showing or if the message showing is NOT the same as the message I want to send. If the message IS the same as the one being shown to the user, I don't want the message to go through (because that is pointless).

To work towards this goal, I found this post on how to only show a toast if none are being shown.

I have modified the code to fit both requirements.

private Toast toast;

public void showAToast (String st, boolean isLong){
    try{
        toast.getView().isShown();
        String text = ((TextView)((LinearLayout)toast.getView()).getChildAt(0)).getText().toString();
        if(!text.equalsIgnoreCase(st)){
            //New message, show it after
            if(isLong){
                toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_LONG);
            } else {
                toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_SHORT);
            }
            toast.show();
        }
    } catch (Exception e) {
        //New message
        if(isLong){
            toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_LONG);
        } else {
            toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_SHORT);
        }
        toast.show();
    }
}

My issue is that any message will not go through if the last toast message was the same as the message that wants to go through.

Not sure exactly why this occurs, but I put some debugging messages in the method to figure out what the issue was.

The messages say that toast.getView().isShown() does not throw the exception (suppose to mean no toast is shown) if any toast message has been sent in the app's lifetime.

So my question is, how can I work around this? Surely there must be a way to achieve this desired functionality.

I saw this before in stackoverflow, but it's not nearly as clean as I would have liked. We implemented a dual toast approach, where it alternates between two toasts. First we define the toasts for the activity prior to the OnCreate:

Toast toast0;
    Toast toast1;
    private static boolean lastToast0 = true;
    In the OnCreate:

    toast0 = new Toast(getApplicationContext());
    toast0.cancel();
    toast1 = new Toast(getApplicationContext());
    toast1.cancel();
    //And finally, when I need to display the toast and cancel the prior toast at the same time I use something similar to:

            if (lastToast0) {
                toast0.cancel();
                toast1.setDuration(Toast.LENGTH_LONG);
                toast1.setText("new message");
                toast1.show();
                lastToast0 = false;
            } else {
                toast1.cancel();
                toast0.setDuration(Toast.LENGTH_LONG);
                toast0.setText("new message");
                toast0.show();
                lastToast0 = true;
            }
   // If you need to just cancel an existing toast (before it times out) use:

                toast0.cancel();
                toast1.cancel();

Quotation

You can use the same Toast instance to show message. If the message is same, the toast will no show twice time, otherwise the text will simple change to the latest one.

Toast mToast;

public void showToast(CharSequence message, int during){
    if (mToast == null) {
        mToast = Toast.makeText(getApplicationContext(), message, during);
    } else {
        mToast.setText(message);
    }
    mToast.show();
}

--↓---↓----↓---update--↓----↓---↓--↓

Sorry about that I miss your point above.

I read the source of Toast that we cannot get the view status since the view had been add to the WindownManager.So far, I cannot find out a method to point whether the Toast is shown.

But you can achieve your own Toast use Service,which likes a toast shown above the application. it may be easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM