One possibility is to make the Toast global
in your Fragment/Activity. Now you can call Toast.cancel() in a callback like onPause()
, which is called if you switch an Activity or Fragment.
The docs say about cancel:
Close the view if it's showing, or don't show it if it isn't showing yet. ...
A nicer way is to make a custom class for the Toast like jitain sharma suggested in the comments.