Finish the calling activity when AsyncTask completes

岁酱吖の 提交于 2019-11-29 03:44:47
((Activity)mContext).finish();

Would be the correct way to cast a Context to an Activity and call its finish() method. Not sure why you'd want to finish an Activity from an AsyncTask though

What you can try to do instead of calling context.finish(), why don't you do a callback interface like this:

public interface TaskCallback{
void done();
}

Then you implement this into your Activity

public Hello extends Activity implements TaskCallback{

    .....BUNCH OF ACTIVITY CODE.....

public void onCreate(Bundle savedInstanceState) {

    MyTask mt = new MyTask(this);
    mt.execute();
}

public void done() {
     finish();
}

}

And instead of having Context as a parameter you have TaskCallback

public class MyTask extends AsyncTask<Void, Void, Void> {
private TaskCallback mCallback;

public MyTask(TaskCallback callback) {

    mCallback = callback;
}  

//doinbackground, etc

    protected void onPostExecute() {
    mCallback.done();

}

There you go, it gives you more flexibility to custom each implementation.

I got the same situation, then I do as follows:

public class MyTask extends AsyncTask<Void, Void, Void> {
private Activity mActivity;
private Context mContext;

public MyTask(Activity activity) {
    mActivity = activity;
    mContext = mActivity.getApplicationContext();
}  

//doinbackground, etc

protected void onPostExecute() {
    mActivity.finish();

}

Hope it help :)

Neeraj

Define a method in your activity class like this:

public void FinishAfterAsyncTask()
{
   this.finish();
}

And call this method from the OnPostExecute method of the AsynTask class.

You could create a new private AsyncTask extended from your public one.

In this private AsyncTask you have access to the Activity stuff and you can override the onPostExecute method to finish it.

Your truly AsyncTask

public class MyPublicAsyncTask extends AsyncTask<Void, Void, Void> {
    Context context;
    public GetHorariosAsyncTask(Context ctx){
        context = ctx;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // prepare yourself for an async work
    }
    @Override
    protected Void doInBackground(Void... params) {
        // Do yout cool async stuff
    }
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
       // you're finish, let's tell to user
    }
}

Your Activity with private AsyncTask

public class MyActivity  extends Activity {
    Activity mAct;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        mAct = this;
    }

    private class MyPrivateAsyncTask extends MyPublicAsyncTask {

        public MyPrivateAsyncTask(Context ctx) {
            super(ctx);
            // TODO Auto-generated constructor stub
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            // our async task is completed! let's take care of this activity
            mAct.finish();
        }

    }
}

Can you try

this.finish()

Seems like its because of calling it using mContext that it says undefined.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!