Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android exit with toast

I have a quit() method that I am using. I use a toast to print out "thank you for using this app". On the next line I do a system.exit(0); and when I run the app, the toast doesn't show before the system exits the app. Is there a way to fix this?

like image 691
Justin Esders Avatar asked Apr 19 '26 20:04

Justin Esders


2 Answers

The way you should do it, in your activity:

Toast.makeText(this,"Thanks for using the app",Toast.LENGTH_SHORT).show();
finish();

Forget System.exit

like image 102
Androrider Avatar answered Apr 21 '26 10:04

Androrider


You could run a thread that waits for the duration of your Toast.-

Thread thread = new Thread(){
    @Override
    public void run() {
        try {
            Thread.sleep(WAIT_TIME_IN_MILLIS);
        } catch (Exception e) {
            e.printStackTrace();
        }  
    }
};

thread.start();
like image 37
ssantos Avatar answered Apr 21 '26 09:04

ssantos