Scorpy86
Scorpy86

Reputation: 339

Android: how to show a toast before forcing the closure of the app?

i've to show a toast and after to force the closure of the app if there isn't an internet connection, but if i show only the toast everything is ok:

Toast.makeText(getApplicationContext(), "Nessuna connessione", Toast.LENGTH_LONG).show();

if i make this:

Toast.makeText(getApplicationContext(), "Nessuna connessione", Toast.LENGTH_LONG).show();
android.os.Process.killProcess(android.os.Process.myPid());

the app is closed and the toast isn't show.

how i can show this toast and after close the app?

Upvotes: 0

Views: 1590

Answers (2)

Achiever
Achiever

Reputation: 1636

First show the Toast:

Toast toast = Toast.makeText(getApplicationContext(), "Message here", Toast.LENGTH_SHORT);
toast.show();

and then call finish:

finish();

Upvotes: 2

M-Wajeeh
M-Wajeeh

Reputation: 17284

Instead of killing the process you can just call finish() in you Activity. This way your toast will be shown.

Upvotes: 2

Related Questions