How to pass context in Async Task class which is coded in different java file from Main Activity but it called from main activity?
Below is
You can just pass the context in the constructor of your AsyncTask.
MyAsyncTask.java
public class MyAsyncTask extends AsyncTask {
private final Context mContext;
public MyAsyncTask(final Context context) {
mContext = context;
}
}
and then just use the mContext variable in your onPostExecute() method.
When you call your AsyncTask from your MainActivity, you pass the context to the constructor of MyAsyncTask.
MainActivity.java
final MyAsyncTask task = new MyAsyncTask(getApplicationContext());
task.execute();