Finish activity after toast message disappears?

前端 未结 12 1091

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

    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.

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

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

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

    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.

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

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

    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.

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

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