Neither Android AsyncTask, onPreExecute(), nor doInBackground() will run.

Asked 1 years ago, Updated 1 years ago, 95 views

Hi, everyone. I'm leaving a question for the first time.

Run AsyncTask on the main thread as follows:

        FetchPlacesListTask placesListTask = new FetchPlacesListTask(this);
        placesListTask.execute(category);

I want to connect the list of places I received from DB with an adapter as follows.

   public class FetchPlacesListTask extends AsyncTask<String, Void, List<Place>> {

        private final Context mContext;
        private List<Place> listPlaces;

        public FetchPlacesListTask(Context context) {
            mContext = context;
        }

        @Override
        protected List<Place> doInBackground(String... params) {
            Log.v("AsyncTask", "doInBackground");

            placeDAO = new PlaceDAO(mContext);
            listPlaces = placeDAO.getPlacesByType(params[0]);

            return listPlaces;
        }

        @Override
        protected void onPostExecute(List<Place> places) {
            if (places != null) {
                adapter = new ListPlacesAdapter(mContext, places);
                listViewPlaces.setAdapter(adapter);
            }
            placeDAO.close();
        }
    }

However, I checked that fetchPlacesListTask is created, but doInBackground() or onPostExecute() is not running. What's the problem?

android android-asynctask

2022-09-22 13:06

1 Answers

You've already got answers, so it's not a solution to the question, but I've added something to check when you use AsyncTask.

AsyncTask uses SerialExecutor for thread management from Honeycomb and above. Therefore, when multiple AsyncTasks are run, the following AsyncTasks are not run unless the previous AsyncTasks are terminated. If the thread is not running, it is also a good idea to check the possibility for this part. (Parallel execution possible through settings)

And if you read the part called Threading rules in the link below, it will help you write the code.

A summary of AsyncTask's history and problems and solutions


2022-09-22 13:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.