0

I use Toast dialogs a lot throughout my application. However, I have noticed that after switching activities, the dialog will continue to stay visible until its timer has run out.

Toast.makeText( getApplicationContext(), R.string.toast_need_bt, Toast.LENGTH_LONG ).show();

I use Toast.LENGTH_LONG because the message is long and if the user decides to read it, the longer time option is needed. However once the user has used the application once or twice they will not need to read the toast messages and they will quickly move from activity to activity. However, these toast dialogs stay on the screen even while switching from activity to activity.

Is there a way to end all Toast Dialogs if the current Activity is terminated?

JuiCe
  • 4,132
  • 16
  • 68
  • 119

3 Answers3

2

Call cancel() on the toast object when finishing/leaving the activity Here is a link to the documentation Toast

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • OK, so I can't just call `Toast.makeText()` as I have shown in my question? `Toast.cancel()` will not work as it is not static. I need to create a separate `Toast` object for each message? – JuiCe Jan 03 '13 at 21:29
  • Correct. You need an object to call `cancel()` on – codeMagic Jan 03 '13 at 21:32
  • If the toasts are that long then you may consider putting them in a custom dialog or class with a dialog theme and provide a checkbox to not show them. Kind of as Robert suggested but then I would normally put it in a dialog as I said – codeMagic Jan 03 '13 at 21:40
1

Toast.makeText returns a Toast object. Call cancel() on this object to cancel it. Check this post for more answers on this topic How to cancel Toast

Community
  • 1
  • 1
Marcin S.
  • 11,161
  • 6
  • 50
  • 63
  • OK, so I can't just call `Toast.makeText()` as I have shown in my question? `Toast.cancel()` will not work as it is not static. I need to create a separate `Toast` object for each message? – JuiCe Jan 03 '13 at 21:28
1

Even though there are a few answers already on how to use the .cancel() method, i would like to add a few options to this usecase:

1) Create in layout notifications Cyril Mottier's Article here

2) Display the toast only the first X times

3) Create a dialog with a "Show notifications" checkbox to allow the user to opt out.

Robert Estivill
  • 12,369
  • 8
  • 43
  • 64