Fire event on AsyncTask finished

Eric picture Eric · Jan 25, 2013 · Viewed 7.6k times · Source

My app has a login activity and it checks the credentials via an internet website. To do so, I (have to) use an AsyncTask class. When login is successful, a global variable is set to true.

Because this is asynchronous, the activity won't wait for the result so I would like to add an event that is fired by the post-execute method of the AsyncTask class. Then a listener on the login activity will close itself and the main activity is visible.

I would like to know if and how this is possible. I have tried some examples from other posts, but can't figure it out from those.

I think I have to do the following: - create an interface in the AsyncTask class - from the post_execute method of that class, I raise the event, but how? - put a listener in the login activity, but how?

any help is appreciated.

Regards, Eric

Answer

Sam picture Sam · Jan 25, 2013

I would like to add an event that is fired by the post-execute method of the AsyncTask class. Then a listener on the login activity will close itself and the main activity is visible.

onPostExecute() is already a callback, you could create another callback like you described but it is unnecessary. Simply pass a reference of your login Activity to your AsyncTask then use it to call finish() and startActivity() in onPostExecute().