AsyncTask ; doInbackground() not called after calling method onPreExecute()

You should execute your task with executor

task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

Because in lower versions of Android all AsyncTasks were executed at single background thread. So new tasks might be waiting, until other task working.

In lower versions in Android (actually on pre-HONEYCOMB) you can't execute AsyncTask on executor.

Change your code to

public void executeAsyncTask()
{
    AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Log.e("AsyncTask", "onPreExecute");
        }

        @Override
        protected String doInBackground(Void... params) {
            Log.v("AsyncTask", "doInBackground");
            String msg = null;
            // some calculation logic of msg variable
            return msg;
        }
        @Override
        protected void onPostExecute(String msg) {
            Log.v("AsyncTask", "onPostExecute");
        }
    };

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB/*HONEYCOMB = 11*/) {
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    } else {
        task.execute();
    }
}

Doing this not working for you?

task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

I was already doing this and the problem was still there, the problem was I was using AsyncTask to run long background tasks that do run forever, so the ThreadPoolExecutor's worker threads were already occupied.

In my case AsyncTask.THREAD_POOL_EXECUTOR (use debugger to get your executor properties) was having the ff properties:

maximumPoolSize = 3
largetsPoolSize = 2
corePoolSize =2 

So disabling one of the long running asyncTasks made my doInBackground code to run but that is not the right solution.

The right solution is I moved long running tasks into custom threads rather than using AsyncTask.


Quoting Android Documentation for Async Task

When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.

Use this snippet for triggering AsyncTask:

if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB)
   asyncTaskInstance.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
else
   pdfPageChanger.execute(params);

I was in the same situation.

  1. I made a call AsyncTask.cancel(boolean mayInterruptIfRunning) passing true
  2. Then I assumed (I was wrong) that the doInBackground() of this task was now interrupted

By adding logs statements I realised that the very first asyncTask was still running ... thus preventing another asyncTask to execute. It's doInBackground() method was running an infinite while loop so I simply had to change condition to consider all the possible "break" cases...