Saqib
Saqib

Reputation: 1129

Wait for AsyncTask to be completed before 'PROCEEDING' in process

In my java code I have an event on button click, that it performs some actions, which are below

OnClick -> AsyncTask [OpenReader] -> if(not opened, return) -> else Proceed to process of onClick

What actually happens is a button is clicked, say there is a method need to be processed in that click, but before that process is initiated I have to execute/check openReader that is actually in between that process initiation and is an AsyncTask, so what happens is when I click openReader call initiated and before that is completed my process after that is started which shouldn't start until finish of AsyncTask, now if I wait for AsyncTask via while loop to finish it hangs the GUI, now what is the possible solution for this to handle?

@Override
public void onClick(View v) {

    logMsg("....", R.color.GREEN);
    String phNum = phoneNumber.getText().toString();

    // Check if phone number is valid
    if (!checkNumber(phNum)) {
        required.setTextColor(Color.RED);
        validNum = false;
        return;
    }

    isReady = false;

    // Prepare device for use
    new openReader().execute();

            // Have to wait for openReader to finish here
            // and then proceed

    int id = v.getId();
    switch (id) {
    case R.id.btnRead:
        flag = 1;
        // Process to be initiated here
        break;
    default:
        break;
    }
}

Upvotes: 2

Views: 1495

Answers (2)

Mehul Joisar
Mehul Joisar

Reputation: 15358

Solution: just pass your clicked view's id along with the constructor of openReader class and then inside onPostExecute method you can use switch case.

Example:

int id = v.getId();
new openReader(id).execute();

and this is how your openReader class will look like,

private class openReader extends AsyncTask<Void, Void, Void>
{
      private int selected_v_id;

public openReader(int i) {
        // TODO Auto-generated constructor stub
        selected_v_id= i;
    }

@Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);


               switch (selected_v_id) {
               case R.id.btnRead:
                      flag = 1;
                    // Process to be initiated here
               break;
               default:
               break;
           }


    }


}

Edit:

if you want to clear the mess inside onPostExecute method, you can do something like this,

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
        performAction(selected_v_id);
}



private void performAction(int x)
{
               switch (selected_v_id) {
               case R.id.btnRead:
                      flag = 1;
                    // Process to be initiated here
               break;
               default:
               break;
           }

}

I hope it will be helpful !!

Upvotes: 1

Karakuri
Karakuri

Reputation: 38585

See AsyncTask.onPostExecute(). Place any code that needs to run after the AsyncTask completes inside this method.

Upvotes: 3

Related Questions