Does anybody know, if there is a possibility to do something (in my case finish activity) on toast message will be closed?
14 Answers
You do that simply by creating a Thread
that lasts as long as the Toast
is displayed and then you can finish your Activity
.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// your other stuff
Toast.makeText(this,"This is a Toast", Toast.LENGTH_LONG).show();
thread.start();
}
Now create use a Handler that waits for (LENGTH_LONG = 3.5) or (LENGTH_SHORT = 2) seconds
Handler().postDelayed({...},Toast.LENGTH_LONG * 1000);

- 67,150
- 23
- 161
- 242
-
2Great ! Thx :) So you can just add Toast.LENGTH_LONG to the sleep method instead, to have the exact match – Hubert Solecki Jun 11 '15 at 08:23
-
RIP for those who down-voted without mentioning the reason :) – Lalit Poptani Jun 19 '17 at 04:37
-
2new thread is overkill here, at least you can use Handler().postDelayed({...},Toast.LENGTH_LONG ). Also, probably, you will have memory leak on configuration change using new thread in such way – Alexey Osminin Feb 19 '19 at 15:33
-
alexey. . . share example plz. – user6159419 Jun 01 '20 at 20:55
-
1`Thread.sleep()` takes a millisecond value, and `LENGTH_LONG` equals 1. So the thread will sleep for 1 millisecond, not for 3,5 seconds. I'm afraid this code will not do what you expect it to do. – mthmulders May 30 '22 at 13:31
-
That's right @mthmulders. cause it's 1 ms the activity will finish instantly – Thelouras Feb 01 '23 at 09:48
-
1@Thelouras Thanks for expecting spoon feeding. The idea is to give hint for the solution. You can easily calculate duration with some simple maths! – Lalit Poptani Feb 03 '23 at 10:54
-
@LalitPoptani But this is not a hint, the author says that the code will run exact the same time as the toast. But this is wrong cause `Thread.sleep()` takes a millisecond value, and `LENGTH_LONG` equals 1. So the thread will sleep for 1 millisecond, not for 3,5 seconds. – Thelouras Feb 05 '23 at 16:38
It should first display Toast
then after 2 seconds, it will finish your activity
.
Toast.makeText(YourActivity.this, "MESSAGE", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
YourActivity.this.finish();
}
}, 2000);

- 2,275
- 1
- 25
- 25
android.widget.Toast doesn't offer any listeners for informing when it is finished.
You may call Toast.getDuration() to learn how long it will last, and make your own TimerTask to run at the time when Toast vanishes, and do your tasks there.

- 39,597
- 13
- 90
- 111
Yes, but this is a trick way
Android Toast doesn't have a way to set a callback after it finished.
So what you can do is based on this fact
private static final int LONG_DELAY = 3500; // 3.5 seconds
private static final int SHORT_DELAY = 2000; // 2 seconds
now you can do:
- Set up the toast
- Set up a counter thread based on the LENGTH_LONG (3.5s) or LENGTH_SHORT(2s) to close the activity.
- toast.show() and thread.start();

- 2,778
- 1
- 18
- 17
-
Don't make assumptions about delays! These may change in different API version. – Pointer Null Sep 30 '11 at 07:56
-
-
3Maybe you're right, getDuration returns only LENGTH_SHORT or LENGTH_LONG, which are constants 0 / 1. Not real time in ms. – Pointer Null Sep 30 '11 at 09:32
Here's how I do it...
Note that this class includes a call to close down the activity that called it. You can take that out if needed.
Also, note that the sleep times track the toast duration, but i've added an extra half second to give a little margin before the activity is ended.
public class Toaster implements Runnable
{
Context theContext;
CharSequence theMessage;
int theDuration;
Activity theActivity;
public Toaster( Activity a, Context c, CharSequence s, int i )
{
theActivity = a;
theContext = c;
theMessage = s;
theDuration = i;
}
@Override
public void run()
{
Toast toast = Toast.makeText(theContext, theMessage, theDuration );
toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
Thread t = new Thread( new Runnable()
{
@Override
public void run()
{
try
{
Thread.sleep(theDuration == Toast.LENGTH_SHORT ? 2500 : 4000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
theActivity.finish();
}
});
t.start();
}
}
In the activity, there's a chunk of code that looks like this, to call it:
Context c = getApplicationContext();
CharSequence msg = "Form Data Submitted!";
int duration = Toast.LENGTH_SHORT;
runOnUiThread( new Toaster(this, c, msg, duration) );

- 920
- 11
- 22
Actually there are no callbacks when a Toast
is being finished, but if you need to know, when will it be closed, you can start a background thread that will sleep a number of milliseconds equal to the Toast
duration, and then execute the needed operation. This is just one way of solving the issue, I'm sure there are more solutions. Hope this helps.

- 39,695
- 10
- 113
- 130
I'm not sure what your use case is, but do you really need to wait for the toast to close to finish your activity?
In my case, I have an activity that is an entry point into the app from a url (allowing the app to be opened from a link in an email or on a web page). If the url doesn't pass a validation check, I show a toast and finish the activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
if (!validateUrl()) {
Toast.makeText(this, R.string.invalid_url, Toast.LENGTH_LONG).show();
finish();
return;
}
...
}
This shows the toast and I don't have to wait until it's no longer displayed before calling finish(). Initially, I thought this wasn't working, but then I discovered it was because I forgot to call show() on the toast!

- 479
- 5
- 9
I've just make a simple library for that "issue" Download:
https://github.com/mkiisoft/Toaster
and use it this way:
Toaster.getInstance().makeText(context, "your custom message", Toast.LENGTH_SHORT, new OnToasterFinish() {
@Override
public void finish() {
// Your code over here after the Toast
}
});

- 7,165
- 2
- 34
- 52
I'm not sure you can do this with Toasts, however, you could replace the toast by a simple dialog (and reuse the Toast design if you want), and then you could use methods such as onDetachedFromWindow
to hook the closure of the activity to the window's.

- 50,022
- 30
- 123
- 131
As of API level 30, there is an addCallback
method for the Toast class. See here: addCallback documentation

- 1,236
- 15
- 18
Since API 30 Toast.Callback is available: https://developer.android.com/reference/android/widget/Toast.Callback
Example:
val toast = Toast.makeText(requireContext(), "wow", Toast.LENGTH_LONG)
toast.show()
toast.addCallback(object : Toast.Callback() {
override fun onToastShown() {
super.onToastShown()
Log.d("toast_log", "toast is showing")
}
override fun onToastHidden() {
super.onToastHidden()
Log.d("toast_log", "toast is hidden")
}
})

- 333
- 3
- 10
Afaik the INotificationManager API (which is used under the hood of the toast class) does not have any support for notifying the caller when it closes the Toast.
There's also no way to check if the Toast is showing or hidden without using reflection to pick out the inner class that represents the transient notification.

- 16,853
- 4
- 55
- 52
Extend the Toast-Class and use your own Callback.

- 5,070
- 2
- 28
- 51
-
don't think this is an option... Toast uses INotificationManager, so there's no way to notify the caller. – Sander Versluys May 14 '12 at 11:37