0

I am trying to make my android app delay for 2 seconds.

I used handler, but the problem is that the handler didn't stop the remaining code execution so I used like below but it didn't work

final Handler handler = new Handler();
public int delay=0;

handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        // Do something after 2s
        delay=delay+1;
    }
}, 2000);

while(delay==0){
}
Niraj
  • 903
  • 8
  • 23
kaarido
  • 3
  • 3
  • Does this answer your question? [How to run a Runnable thread in Android at defined intervals?](https://stackoverflow.com/questions/1921514/how-to-run-a-runnable-thread-in-android-at-defined-intervals) – Vishal Jan 20 '20 at 03:44

1 Answers1

1

Alright, so handler.postDelayed(Runnable, 2000) would delay the Runnable method those 2 seconds. That would be done without blocking the application's main thread from running for those 2 seconds.

But to delay the whole application, you will want to block the main thread. So, you will want to do the following:

Thread.sleep(2000)

That will block the main thread from running for 2 seconds, and accordingly, the whole application.

Rajesh Satvara
  • 3,842
  • 2
  • 30
  • 50
Vimukthi Sineth
  • 310
  • 2
  • 8