Вопрос

Why does my AsyncTask block the UI Thread?

My application becomes unresponsive while the AsyncTask is taking place.

This is called in the UI Thread:

new AsyncFetch().execute();

This is my AsyncTask class:

class AsyncFetch extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... args) {
        String data = null;
        try {
            data = DataHandler.httpFetch(DataHandler.API_URL);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return data;
    }

    protected void onPostExecute(String data) {
        DataHandler.AsyncFetchResult(data);
    }

}
Это было полезно?

Решение

onPostExecute runs on the UI thread. Avoid doing too much work here. Do the heavy stuff in doInBackground and then just update the UI (or whatever you need to do) in onPostExecute.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top