Hi I wanted to add a delay on my splash screen using a Async Task but I can’t figure out how should I do it. Here’s my DoInBackground code so far:
@Override
protected Integer doInBackground(String... params) {
Thread SplashThread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while(running && (waited < delayTime)) {
sleep(100);
if(running) {
waited += 100;
}
}
} catch(InterruptedException e) {
// do nothing
} finally {
finish();
stop();
}
}
};
SplashThread.start();
return null;
}
I think the part of the return null is the main cause of problem since it’s ending up my Async Task even my SplashThread is still running in background leaving a leak on thins part onwards which lead to error. I also tried to use counDownTimer as a subtitute but this leads in the same problem. Any ways to do this properly?
You don’t need the additional thread that you start in the
doInBackgroundmethod of yourAsyncTask. TheAsyncTaskitself runs thedoInBackgroundmethod in another thread, also,because of this the
whileloop it’s most likely unnecessary. Do the work you want to do in thedoInBackgroundmethod and then simply return. If you want/need some additional delay(although you should avoid this at all costs) you can use theThread.sleepmethod to pause the thread before returning.