Finish activity after toast message disappears?

前端 未结 12 1090

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

相关标签:
12条回答
  • 2020-12-01 10:56

    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.

    0 讨论(0)
  • 2020-12-01 10:56

    Extend the Toast-Class and use your own Callback.

    0 讨论(0)
  • 2020-12-01 10:58

    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!

    0 讨论(0)
  • 2020-12-01 11:07

    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();
    0 讨论(0)
  • 2020-12-01 11:07

    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) );
    
    0 讨论(0)
  • 2020-12-01 11:09

    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);
    
    0 讨论(0)
提交回复
热议问题