Sometimes Toast doesn't disappear and remains visible even after I terminate App

Viewed 447

I have a very simple Toast shown under certain circumstamces:

if (!this.isFinishing()) {
    Toast.makeText(this, R.string.my_toast_message, Toast.LENGTH_LONG).show();
]

Most of the times the Toast is shown and fades away as supposed but sometimes remains always visible and even if you terminate the App the Toast doesn't disappear.

This is a problem that occurs sporadically but when It happens It is very annoying and the only way to make the Toast fading away is rebooting the system or doing whatever thing that could show another Toastof whatever App.

This looks like a problem of Android itself rather than App since I have seen this issue happening on different devices and different Apps that use Toast.

I'm wondering if there is some workaround to prevent this issue.

2 Answers

Toasts are meant to be system level notifications, so they won't disappear even if the app is killed. If you call show for several toasts they will show one after another. If the same toast is called multiple times, it'll give the impression that a toast will take a long time before fading away.

For app notifications, consider using Snackbars instead.

Please refer to this question.

If you're creating the Toast in an asynchronous call, based on this answer, Toasts can get stuck when called from a thread that finishes and is killed before the Toast has a chance to disappear. If you're calling it in a separate thread, you may want to consider calling it from the main thread instead.

Related