18

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 ?

dark_shadow
  • 3,503
  • 11
  • 56
  • 81

7 Answers7

55

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

Uday
  • 5,933
  • 9
  • 44
  • 76
8

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
Mats Hofman
  • 7,060
  • 6
  • 33
  • 48
3

For Kotlin

 Handler().postDelayed({
            val i = Intent(this, MainActivity::class.java)
            startActivity(i)
        }, 5000)
kuzdu
  • 7,124
  • 1
  • 51
  • 69
3

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.

NotACleverMan
  • 12,107
  • 12
  • 47
  • 67
1

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
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
0

Simply set the layout!

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

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

            }
        }, 5000);
Mubi Ali
  • 83
  • 1
  • 7
  • Bad solution. After 5 seconds, the activity may no longer exist, or it may be recreated. For example, due to a change in orientation. – Enyby Jan 26 '20 at 16:52
0

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);
Nick B
  • 423
  • 3
  • 6