Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 765
Hein du Plessis Avatar asked Aug 31 '12 11:08

Hein du Plessis


3 Answers

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.

like image 172
Ogulcan Orhan Avatar answered Nov 07 '22 18:11

Ogulcan Orhan


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

like image 10
waqaslam Avatar answered Nov 07 '22 19:11

waqaslam


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;
}
like image 2
Chintan Raghwani Avatar answered Nov 07 '22 18:11

Chintan Raghwani