0

I have the following design in my app

  • there is a BaseActivity where I perform some common tasks
  • All other activities extend BaseActivity so as I need not replicate the code for common tasks across all other activities.
  • One such common task defined in BaseActivity is a login operation where a user logs in to his/her account. Since there are some backend database operations involved, I am using AsyncTask while writing those methods.

Now my problem is that in my child activities, if I invoke the login method of BaseActivity, then how do I pass the control back to my child activity. Login method can be called by any activity across my app, hence every activity will need a confirmation back from BaseActivity about the successful login so as specific tasks in those child activities can be performed.

Any suggestion/help is very much appreciated

Martin Cazares
  • 13,637
  • 10
  • 47
  • 54
user1938357
  • 1,466
  • 3
  • 20
  • 33
  • See [This answer](http://stackoverflow.com/questions/18517400/inner-class-can-access-but-not-update-values-asynctask/18517648#18517648) the bottom part is about using an `interface` to send a callback when the `AsyncTask` has finished. Implement this in the `Activities` that will need it – codeMagic Sep 06 '13 at 22:51

1 Answers1

1

You can have two methods in the BaseActivity class saying performLogin() and postLogin() like following:

public void performLogin()
{
  // custom code    
  LoginTask task = new LoginTask(this); // pass reference of the activity to Asyntask
  // execute task
}

LoginTask in this has a constructor which accepts parameter of type BaseActivity. Have your AsyncTask (LoginTask in the example above) then call the activity.postLogin() method. You can override this method as per your child activity's needs.

mou
  • 323
  • 3
  • 10