DenWav
DenWav

Reputation: 53

Android - AsyncTask - onPreExecute() runs after doInBackground() is complete

I'm trying to have a ProgressDialog appear when the AsyncTask starts in onPreExecute(), do some thing in doInBackground(), and close the ProgressDialog in onPostExecute().

Everywhere I look this is supposed to be easy and straight forward, but I must be doing something wrong.

Whenever I click the Button in my app to update and the AsyncTask is run the app freezes up, waits a bit, then the ProgressDialog instantly appears and disappears, with the work in doInBackground() completed. I don't understand why onPreExecute() is being run after doInBackground(), but that seems to be exactly what is happening here. Here is the code:

public class UpdateStats extends AsyncTask<CatchAPI, Void, String[]> {

    private ProgressDialog pdia;
    private Context context;
    private APIParser parser = new APIParser();

    public UpdateStats(Context contexts) {
        context = contexts;
    }

    @Override
    protected void onPreExecute() {
        pdia = new ProgressDialog(context);
        pdia.setMessage("Loading...");
        pdia.show();
        super.onPreExecute();
    }

    @Override
    protected String[] doInBackground(CatchAPI... api) {

        String apiOutput = api[0].returnAPI();

        return new String[] {
                parser.getPoolName(apiOutput), 
                parser.getHashRate(apiOutput), 
                parser.getWorkers(apiOutput), 
                parser.getSharesThisRound(apiOutput)
            };
    }

    @Override
    protected void onPostExecute(String[] result) {
        pdia.dismiss();
        super.onPostExecute(result);
    }

}

And the onClickListener in the MainActivity:

updateButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        String[] text = null;
        UpdateStats updater = new UpdateStats(context);
        try {
            text = updater.execute(api).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        poolNameTV.setText(text[0]);
        hashRateTV.setText(text[1]);
        workersTV.setText(text[2]);
        sharesThisRoundTV.setText(text[3]);
    }
});

Upvotes: 1

Views: 6784

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

In Here :

text = updater.execute(api).get();  //<<<

you are calling AsyncTask.get() which freeze UI Thread execution until doInBackground execution not complete. execute AsyncTask as :

updater.execute(api)

and update UI elements inside onPostExecute which called on UI Thread when doInBackground execution complete.

@Override
protected void onPostExecute(String[] result) {
    pdia.dismiss();
    // update UI here..
    poolNameTV.setText(result[0]);
    hashRateTV.setText(result[1]);
    workersTV.setText(result[2]);
    sharesThisRoundTV.setText(result[3]);

    super.onPostExecute(result);

}

Upvotes: 7

Related Questions