Android AsyncTask in external class

android, android-asynctask, asynchronous

Solution

Instead of passing 2 parameters pass only one like this:

public class Test101 extends Activity {
    private Button btnLogin;
    private LoginTask mLoginTask;

    mLoginTask=new LoginTask(this);
    mLoginTask.execute(); 
    //Instead of sending null put the 1st parameter of the AsyncTask as Void

AsyncTask Code:

  Activity mParentActivity;

public LoginTask(Activity act){
    this = act;
}

onPostExecute(..){
    ((Test101)mParentActivity).callSomeMethod();
}

Problem

I've been working on an app and I've managed to get the `AsyncTask` to work fine when it's in an inner class. Now, I am refactoring the code so that the `AsyncTask` is in a separate class of its own, but I am wondering, how do I kill the `ProgressDialog` and start a new Activity once the task is completed successfully? I've tried starting a new Activity in the `onPostExecute(..)` method, but I know that won't work. Passing my UI thread activity as an argument in the constructor for the AsyncTask did not seem to work: ``` //In UI Thread I had public class Test101 extends Activity { private Button btnLogin; private LoginTask mLoginTask; private Context context=this; private Test101 mTest101; mLoginTask=new LoginTask(context,mTest101); mLoginTask.execute(null); // In the AsyncTask I had Activity mParentActivity; public LoginTask(Context context,Activity act){ this.ctx=context; this.mParentActivity=act; } onPostExecute(..){ mParentActivity.callSomeMethod(); } ... } ``` I kept getting a `NullPointerException`, maybe I'm missing something but that didn't work for me.

Original source