user393964
user393964

Reputation:

Notifiy Activity when AsyncTask is ready?

I have this activity that class a Login class inside an onclick event. My Login class basically does a http post and parses the XML response from the server. This happens inside a thread in the constructor of the Login class.

My problem is in this part:

Login login = new TvsLogin(emailEditText.getText().toString(), passwordEditText.getText().toString());
if(login.isErrorOccurd()){

}
else{

}

Basically, while the constructor of login is doing it's magic, the rest of my code gets executed and login.isErrorOccurd() appears false and my else block gets executed, even when there's an error.

Any suggestions how I should fix this? Login can take time so it makes sense to do it in a thread, but it's not working out the way I expected.

EDIT:

Like Pixie says, I'll have to use an AsyncTask. I would still like to do the reporting inside my Activity though. How do I do this? (Is this a good idea, or should I just do this inside the onPostExecute() method in my Activity?)

Upvotes: 0

Views: 862

Answers (1)

Michael
Michael

Reputation: 54715

There's nothing strange in this behavior. Login is not checked in secondary thread when login.isErrorOccurd() method is called. You can use Activity.runOnUiThread() method to notify your activity about login check results or you can use AsyncTask class to let it do this stuff for you. But anyway error checking will be in other part of code.

Upvotes: 1

Related Questions