Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent asynctask from being executed multiple times

I'm calling an asynctask from my UI thread on the click of a button, which performs some HTTP-Get requests.

What would happen if a user clicks the button again before the current task is complete? Is this handled internally by the asynctask, or do I need to take care of it myself?

like image 741
Johan Avatar asked Nov 27 '12 08:11

Johan


People also ask

How many times an instance of AsyncTask can be executed?

AsyncTask instances can only be used one time.

What happen if we call execute more than once in AsyncTask?

Limitation Of AsyncTask There is a limit of how many tasks can be run simultaneously. Since AsyncTask uses a thread pool executor with max number of worker threads (128) and the delayed tasks queue has fixed size 10. If you try to execute more than 138 AsyncTasks the app will crash with java.

What happens to AsyncTask if activity is destroyed?

If you start an AsyncTask inside an Activity and you rotate the device, the Activity will be destroyed and a new instance will be created. But the AsyncTask will not die. It will go on living until it completes. And when it completes, the AsyncTask won't update the UI of the new Activity.

What are the problems in AsyncTask?

In summary, the three most common issues with AsyncTask are: Memory leaks. Cancellation of background work. Computational cost.


1 Answers

There are two Ways to do this.

1: Show a Progress Dialog and block the user for further input.

2: Create you AsyncTask variable in class scope private. Then this will never run more than once.

like image 91
Naeem Avatar answered Sep 22 '22 21:09

Naeem