0

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.

Community
  • 1
  • 1
  • Can you just keep track of the most recent String that was toasted? Then you can just do a simply string comparison with `equals()`. – Code-Apprentice Aug 09 '16 at 02:17
  • Code-Apprentice You're missing the point. The issue is not that the strings are being compared incorrectly, the issue is that the toast.isShown() method is going through if the toast has been sent at any point in the application's lifecycle. This means that, even if no toast is on the screen, it still runs through all of the try block code instead of the catch block code (like it is suppose to). I'm asking for a way to work around this issue to obtain this desired effect. –  Aug 09 '16 at 02:20
  • OIC...yes, I misunderstood the question entirely, but not for the reasons you assumed...my bad. – Code-Apprentice Aug 09 '16 at 02:23

2 Answers2

0

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

Ozan Manav
  • 429
  • 4
  • 18
0

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.

vcokey
  • 11
  • 6
  • This would deny the same message twice in a row, would it not? That is the functionality that the current method already has, not what I want. I want a repeated message to go through, just not if the message is already visible on screen. If there is no message on screen, the message should be sent. –  Aug 09 '16 at 03:09