Finish activity after toast message disappears?
Does anybody know, if there is a possibility to do something (in my case finish activity) on toast messa开发者_开发问答ge will be closed?
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);
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);
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.
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();
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) );
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.
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'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!
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
}
});
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.
Extend the Toast-Class and use your own Callback.
As of API level 30, there is an addCallback
method for the Toast class. See here: addCallback documentation
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")
}
})
精彩评论