Inside doInBackground I need to refer to application context or an activity.
Is there any difference between new MyAsyncTask(getApplicationContext())
and doInBackground(Context... params)
in terms of thread safety and other possible multi-thread concepts, restrictions ?
Thanks.
No. Assuming you have something like:
private class MyAsyncTask extends AsyncTask<Context, Integer, Long> {
private Context _context = null;
MyAsyncTask(Context context) {
_context = context;
}
protected Long doInBackground(Context... context) {
// if _context and context are the same, it doesn't matter
// which you use.
return 0;
}
protected void onProgressUpdate(Integer... progress) {
// update progress
}
protected void onPostExecute(Long result) {
// publish result
}
}
Then there are no inherent issues regarding multi-threading wrt the Context itself.
Context useMe = getApplicationContext();
MyAsyncTask task = new MyAsyncTask(useMe);
task.execute(useMe); // should use this if provided, otherwise, what was in constructor
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With