dark_shadow
dark_shadow

Reputation: 3573

How to put some delay in calling an activity from another activity?

I have an application in which I'm receiving a sms containing his location.On receiving sms it calls another activity to start and passes that location to that activity to plot it on the map.Before calling the second activity it shows a toast like notification on the screen but somehoe due to calling second activity that toast doesn't come up.My question is how can we delay the calling of second activity from this activity ?

Upvotes: 19

Views: 45952

Answers (7)

kuzdu
kuzdu

Reputation: 7524

For Kotlin

 Handler().postDelayed({
            val i = Intent(this, MainActivity::class.java)
            startActivity(i)
        }, 5000)

Upvotes: 4

Nick B
Nick B

Reputation: 423

An example would be the following:

Handler TimeDelay=new Handler();
                if(previous=="geofence"){



                    tts.speak(previous,TextToSpeech.QUEUE_ADD,null, null);
                    Runnable r = new Runnable() {
                        @Override
                        public void run() {
                            /*
                            Intent intent = new Intent(
                                    MyBroadcastMessageReceiver.class.getName());
                            intent.putExtra("some additional data", choice);
                            someActivity.sendBroadcast(intent);*/
                            tts.speak(previous,TextToSpeech.QUEUE_ADD,null, null);
                        }
                    };
                    TimeDelay.postDelayed(r, 150000);

Upvotes: 0

Hasan A Yousef
Hasan A Yousef

Reputation: 24988

Try:

Runnable r = new Runnable() {
        @Override
        public void run() {
           // if you are redirecting from a fragment then use getActivity() as the context.
          startActivity(new Intent(SplashActivity.this, MainActivity.class));
          // To close the CurrentActitity, r.g. SpalshActivity
          finish();
       }
};

Handler h = new Handler();
// The Runnable will be executed after the given delay time
h.postDelayed(r, 1500); // will be delayed for 1.5 seconds

Upvotes: 1

Mubi Ali
Mubi Ali

Reputation: 83

Simply set the layout!

 new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

            setContentView(R.layout.next); //where <next> is you target activity :)

            }
        }, 5000);

Upvotes: 0

Mats Hofman
Mats Hofman

Reputation: 7240

You can do it with a Handler like this

    Handler h = new Handler(){
        @Override
        public void handleMessage(Message msg) {

            Intent i = new Intent().setClass(ctx, MainActivity.class);                  
            startActivity(i);
        }           
    };

    h.sendEmptyMessageDelayed(0, 1500); // 1500 is time in miliseconds

Upvotes: 8

Uday
Uday

Reputation: 6023

You can use something like this:

 new Handler().postDelayed(new Runnable() {
                      @Override
                      public void run() {

                          Intent i=new Intent(SearxhJobs.this,JobsTypes.class);
                          startActivity(i);
                      }
                  }, 5000);

Here it waits upto 5 seconds to launch activity.

Hope it helps

Upvotes: 55

NotACleverMan
NotACleverMan

Reputation: 12237

Make an AsyncClass that does Thread.sleep() in the doInBackground() method, then navigate to your new activity in the your onPostExecute() method.

Call your toast message and then execute the AsyncClass.

Upvotes: 3

Related Questions