Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finish activity after toast message disappears?

Tags:

android

toast

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

like image 218
Tima Avatar asked Sep 30 '11 07:09

Tima


People also ask

Why is toast not showing messages?

Make sure not to forget to call show() after the makeText. Check for the Context , if its the right one. The most important one , make sure your Android Notifications are on for your app, else the Toast will not be shown.

How can we display toast messages in an activity?

Display the created Toast Message using the show() method of the Toast class. The code to show the Toast message: Toast. makeText(getApplicationContext(), "This a toast message", Toast.

How do you display the toast message at the center of the app?

A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.


1 Answers

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();                 }              }              }; 
like image 154
Lalit Poptani Avatar answered Oct 19 '22 08:10

Lalit Poptani