Finish activity after toast message disappears?

前端 未结 12 1106

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: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!

提交回复
热议问题