Does anybody know, if there is a possibility to do something (in my case finish activity) on toast message will be closed?
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.
You call the toast, thn finish using the Onstop(). Your toast now will appear.
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.
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 a thread that waits for (LENGTH_LONG = 3.5) or (LENGTH_SHORT = 2) seconds
Thread thread = new Thread(){
@Override
public void run() {
try {
Thread.sleep(Toast.LENGTH_LONG); // As I am using LENGTH_LONG in Toast
Your_Activity.this.finish();
} catch (Exception e) {
e.printStackTrace();
}
}
};
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.
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
}
});