JuiCe
JuiCe

Reputation: 4201

How to terminate a Toast dialog after switching Activities

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?

Upvotes: 0

Views: 790

Answers (3)

Marcin S.
Marcin S.

Reputation: 11191

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

Upvotes: 1

Robert Estivill
Robert Estivill

Reputation: 12477

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.

Upvotes: 1

codeMagic
codeMagic

Reputation: 44571

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

Upvotes: 2

Related Questions