3

In my Application I have 3 Activities:

1. Login

2. AsyncTask (different class)

3. Welcome

After receiving all the information in the Login Activity it moves to the AsyncTask Activity. When the the AsyncTask is completed it moves to the Welcome Activity. I want to close the Login Activity as soon as the AsyncTask Activity completes its job..

I am trying to use the following code in the onPostExecite() of AsyncTask:

Login.this.finish();

But I am getting an error of: No enclosing instance of the type of Login is accessible in scope..

Please tell me how can i close my Login Activity after completing my AsyncTask..

Thanks in Advance...

Abhishek Dhiman
  • 1,631
  • 6
  • 25
  • 38

2 Answers2

9

As AsyncTaskActivity is in separate file than LoginActivity, you cant finish LoginActivity by:

Login.this.finish();

Instead you should pass the Activity Reference as parameter in the AsyncTaskActivity Class, like you can define following constructor:

private class AsyncTaskActivity extends AsyncTask<String, Void, String> {

Activity mActivity;
    public AsyncTaskActivity(Activity activity)
    {
         super();
         this.mActivity=activity;
    }
}

and whenever you want to finish the activity call

this.mActivity.finish();

Create AsyncTaskActivity's Object in LoginActivity by

AsyncTaskActivity asynTask = new AsyncTaskActivity(LoginActivity.this);
jeet
  • 29,001
  • 6
  • 52
  • 53
  • 1
    I got an error in `AsyncTaskActivity asynTask = new AsyncTask(LoginActivity.this);`
    But it works for `AsyncTaskActivity(this).execute()` with some edition.
    – Ratna Halder Aug 29 '12 at 05:51
  • oh, sorry it was AsyncTaskActivity asynTask = new AsyncTaskActivity(LoginActivity.this); – jeet Aug 29 '12 at 06:10
2

Ok . you should call finish() in onPostExcecute method after stating new activity.

STEPS:

1) Login activity

2) Asynac Task(pass application context)

3) Call new Activity

4) finish your login activity in onPostExcecute( context.finish())

Dinesh Anuruddha
  • 7,137
  • 6
  • 32
  • 45