Tima
Tima

Reputation: 12905

Finish activity after toast message disappears?

Does anybody know, if there is a possibility to do something (in my case finish activity) on toast message will be closed?

Upvotes: 35

Views: 37527

Answers (14)

Lalit Poptani
Lalit Poptani

Reputation: 67286

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)...

    Handler().postDelayed({...},3500);

... or (LENGTH_SHORT = 2) seconds

    Handler().postDelayed({...},2000);

Upvotes: 48

Roman T.
Roman T.

Reputation: 393

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")
                
            }
        })

Upvotes: 3

Carl Smith
Carl Smith

Reputation: 1360

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

Upvotes: 0

Mariano Zorrilla
Mariano Zorrilla

Reputation: 7686

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
                }
            });

Upvotes: 1

Julio Betta
Julio Betta

Reputation: 2295

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);

Upvotes: 19

Vicki
Vicki

Reputation: 539

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!

Upvotes: 1

Mike Fulton
Mike Fulton

Reputation: 918

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) );

Upvotes: 3

Thommy
Thommy

Reputation: 5407

Extend the Toast-Class and use your own Callback.

Upvotes: 0

Thiru
Thiru

Reputation: 526

You call the toast, thn finish using the Onstop(). Your toast now will appear.

Upvotes: 0

Jens
Jens

Reputation: 17077

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.

Upvotes: 0

Terence Lui
Terence Lui

Reputation: 2808

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:

  1. Set up the toast
  2. Set up a counter thread based on the LENGTH_LONG (3.5s) or LENGTH_SHORT(2s) to close the activity.
  3. toast.show() and thread.start();

Upvotes: 9

Egor
Egor

Reputation: 40203

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.

Upvotes: 2

Sephy
Sephy

Reputation: 50392

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.

Upvotes: 1

Pointer Null
Pointer Null

Reputation: 40380

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.

Upvotes: 9

Related Questions