问题
Does anybody know, if there is a possibility to do something (in my case finish activity) on toast message will be closed?
回答1:
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();
}
}
};
回答2:
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);
回答3:
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.
回答4:
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();
回答5:
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) );
回答6:
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.
回答7:
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.
回答8:
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!
回答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
}
});
回答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.
回答11:
You call the toast, thn finish using the Onstop(). Your toast now will appear.
回答12:
Extend the Toast-Class and use your own Callback.
来源:https://stackoverflow.com/questions/7607410/finish-activity-after-toast-message-disappears