0

I'm using the following code to show a longer than usual toast and it's working fine in displaying the wanted text for longer, with no difference in the perception of the user from an usual toast, except for the time.

Toast toast;
CountDownTimer mcd;

[...]

toast=Toast.makeText(getApplicationContext(), 
StaticMethods.giveStringAccordingtoLanguage(action,language), 
Toast.LENGTH_LONG);
mcd=new CountDownTimer(6000, 50)
{

   public void onTick(long millisUntilFinished) {toast.show();}
   public void onFinish() {toast.show();}

}.start();

But when the user exits the app if that custom toast is showing it keeps on showing until its time is finished.

I've tried to solve it with a code like this when the app is exiting:

    try
    {
        toast.cancel();
    }
    catch (Exception e)
    {
    }
    try
    {          
        mcd.cancel();
    }
    catch (Exception e)
    {
    } 

Anyway it doesn't work at all.

I've also tried to redefine the behavior of the ticks with a code like this:

 mcd.onTick(long millisUntilFinished) {toast.cancel();}

 mcd.onFinish() {toast.cancel();}

But that code isn't even correct, it looks that it only is correct if done in the creation of a new CountDownTimer object via new.

Any idea on how this could be solved? On worst case scenario I could just pop up a dialog which won't keep on showing after exiting the app, but I'd prefer to do it this way because I think it would be better for the user.

2 Answers2

0

If cancel() and hide() do not work, you should try implementing a custom view with an animation that looks like a toast for that activity/fragment. That way view can be closed whenever you want and it's only attached to that activity/fragment.

See the following stackoverflow answer

I hope this helps!

0

You say "keeps on showing until its time is finished" -- is this time the normal LENGTH_LONG time or your 6000ms time? If it is the LENGTH_LONG I would not bother, as this is the normal behaviour of a Toast. You could shorten the time if you want by using LENGTH_SHORT though.

If it is the 6000ms, there is something wrong with cancelling the CountdownTimer. Where do you call the mcd.cancel() in your code? In Activity#onDestroy()? Make sure (by debugging) that the mcd.cancel() is called.

EDIT: Toast cumulation

I guess, it might be something known as Toast cumulation. Toasts are not normal views belonging to your app, but are created as remote views. So I suppose, if you call toast.show(), it tells the System to show that view, which creates some kind of copy and shows it. Calling show() again and again enqueues those show requests, and cancel() only cancells the last request.

Long story short: In the onTick() and onFinish() methods of your CountDownTimer, Try to call toast.cancel() directly before calling toast.show(). This should avoid the cumulation (queueing) of the toasts -- I hope it does not flicker, but I doubt it, due to the fade out animation toasts are using.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • It's the 6000 ms. Yes it executes in #onDestroy, I've debugged the code and cmd.cancel(); is executed, but it doesn't seem to be doing anything. – Jesús Gómez Sep 20 '17 at 18:59