Hein du Plessis
Hein du Plessis

Reputation: 3357

Android AsyncTask Better way of accessing Activity Context

It took my quite a while to get this to work, but It's clearly not best practice. In short, I need to show a dialog when my AsyncTask finishes, but getApplicationContext() does not work, neither does passing it as a parameter when creating the AsyncTask. So I've declared a public variable for the context in my AsyncTask class and set it before I execute:

    private OnClickListener clickLoadRefs = new OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d("H","Clicked Load Refs");
        RefreshRefPoints refreshRefPoints = new RefreshRefPoints();
        refreshRefPoints.myCtx=v.getContext();
        refreshRefPoints.execute(v.getContext());
    }
};

private class RefreshRefPoints extends AsyncTask<Context, Integer, Integer> {

    private Integer nPoints=0;
    public Context myCtx;
    private ProgressDialog pd;

    protected Integer doInBackground(Context... ctx) {
        Log.d("H","doInBackground()");
        dbHelper.clearRefPoints();
        requestRefPoints();
        nPoints = parseRefPointsCSV();

        return nPoints;
    }

    protected void onProgressUpdate(Integer... progress) {
    }

    protected void onPreExecute() 
    {
        pd = ProgressDialog.show(myCtx, "Refreshing Reference Points", "Loading...", true,false);
        Log.d( "H", "onPreExecute()" );
    }
    protected void onPostExecute(Integer result) {
        pd.dismiss();
        AlertDialog.Builder builder = new AlertDialog.Builder(myCtx);
        builder.setTitle("Reference points refresh complete");
        builder.setMessage(result+" records loaded");
        builder.setPositiveButton("OK",null);
        builder.show();
        Log.d("H","onPostExecute()");       
    }...etc

Can anybody just show me the proper way of passing the context?

Thanks

Upvotes: 6

Views: 13252

Answers (3)

Ogulcan Orhan
Ogulcan Orhan

Reputation: 5317

Define a constructor method and pass context a parameter. It would be better.

Here what I meant:

private class RefreshRefPoints extends AsyncTask<Void, Integer, Integer> {

    private Integer nPoints=0;
    private Context myCtx;
    private ProgressDialog pd;

    public RefreshRefPoints(Context ctx){
        // Now set context
        this.myCtx = ctx;
    }

}

That's all.

Upvotes: 12

waqaslam
waqaslam

Reputation: 68187

You may also use YourActivityName.this to refer to the activity Context. Because Activites extend Context, so its valid to do so.

Upvotes: 10

Chintan Raghwani
Chintan Raghwani

Reputation: 3370

Pass context in constructor as

private OnClickListener clickLoadRefs = new OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d("H","Clicked Load Refs");
        RefreshRefPoints refreshRefPoints = new RefreshRefPoints(Your_ActivityName.this);
        refreshRefPoints.myCtx=v.getContext();
        refreshRefPoints.execute(v.getContext());
    }
};


private class RefreshRefPoints extends AsyncTask<Void, Integer, Integer> {

    private Integer nPoints=0;
    public Context myCtx;
    private ProgressDialog pd;

public RefreshRefPoints (Context ctx) {
    myCtx = ctx;
}

Upvotes: 2

Related Questions