2

Below code shows my present implementation , here am creating a toast object and closing in onPause(). This works fine until there is no queue for toasts.

public static long **savedTimeStamp=0**;
    Toast showToastMessage;

if(System.currentTimeMillis()/1000 - savedTimeStamp >= 60 || savedTimeStamp==0){
    showToastMessage=Toast.makeText(this.this, "Toast message", Toast.LENGTH_LONG);
    showToastMessage.show();
    savedTimeStamp=System.currentTimeMillis()/1000;
}

    @Override
             protected void onPause() {
                  super.onPause();
                  if(showToastMessage!=null)
                    {
                    showToastMessage.cancel();
                    }
    }
VishnuVS
  • 1,055
  • 2
  • 14
  • 29
Basavaraj Hampali
  • 3,905
  • 1
  • 19
  • 22
  • Until there is no queue? What do you mean? Also, shouldn't you call super.onPause() AFTER canceling the Toast? – LuxuryMode Jan 15 '13 at 04:52
  • @LuxuryMode i would say it does not matter to call super.onPause() after or before the code. Its one and the same thing. – akkilis Jan 15 '13 at 05:02
  • @LuxuryMode here que terms to be toast message which are waiting for there turn . Say you have a zoom-out button and on each time exceding the max limit a toast messsage is displayed . As a user may be i will be clicking 5 times after recching the max limit hence he will be showed 5 toast message which is bad UX. – Basavaraj Hampali Jan 15 '13 at 05:46
  • @akkilis onpause() is used for better UX , i dont want to show user "Activities A" toast message when he moves to "Activity B" , so on each activities onapuse() am killing the toast. If you know any other way plz let us know – Basavaraj Hampali Jan 15 '13 at 05:49
  • A solution in this post: http://stackoverflow.com/questions/2755277/android-hide-all-showed-toast-messages/33739965#33739965 – Viker Nov 16 '15 at 16:24

4 Answers4

6

I find that using only on instance of Toast works best. Allocate it the first time, and the just update it with setText and re-show. Then cancel works as expected because there is only one instance to cancel.

if (showToastMessage != null) {
    showToastMessage.setText("New Toast message");
    showToastMessage.show();
} else {
    showToastMessage = Toast.makeText(showString.this,"First Toast message",Toast.LENGTH_LONG);
    showToastMessage.show();
}
iagreen
  • 31,470
  • 8
  • 76
  • 90
  • dude onpause is used for this reasons: onpause() is used for better UX , i dont want to show user "Activities A" toast message when he moves to "Activity B" , so on each activities onapuse() am killing the toast. – Basavaraj Hampali Jan 15 '13 at 05:59
  • You should still use `cancel` in `onPause()`! I did not say get rid of it. Use the above code when creating so you only ever have one active `Toast` -- hence the note " Then cancel works as expected because there is only one instance to cancel." – iagreen Jan 15 '13 at 06:04
  • yes agree with you . but had a different scenario: Say you have a zoom-out button and on each time exceeding the max limit a toast message is displayed . As a user may be i will be clicking 5 times after reaching the max limit hence he will be showed 5 toast message which is bad UX. – Basavaraj Hampali Jan 15 '13 at 06:18
  • Great answer. Should be the accepted one. Instead Basavaraj accepted his own crappy one. – izolate Dec 12 '13 at 03:11
2

Create a generic class like this:

public class JToast {
private static Toast toast;
private static int LENGTH_LONG=Toast.LENGTH_LONG;
private static int LENGTH_SHORT=Toast.LENGTH_SHORT;

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

You get a toast by just calling

JToast.makeText(context, "My Toast Message", JToast.LENGTH_SHORT).show();
Jitendra Singh
  • 149
  • 2
  • 7
0

Make your own singleton class, having a handler and a LooperThread. Make a method something like showText(text, duration) which creates a runnable inside it.

in run method of it, make an object of Toast with your text, and after that post to handler.

Looper thread will implement the code to see if the handler is null, if yes will create it.

This way, every time you generate a toast message it will be immediately updated on the UI instead of lining up in the queue.

akkilis
  • 1,904
  • 14
  • 21
0

Final conclusion is: Toast in Que cannot be stoped . So avoid using toast in que.

Basavaraj Hampali
  • 3,905
  • 1
  • 19
  • 22