1

I'm using an AsyncTask to perform a login task. The task connects to a PHP script and the returns a JSON object. The JSON object includes a user_id if the authentication authenticates correctly, it also returns a boolean if the login succeeds.

I want the UI thread to run an activity when the boolean returns true, and a different activity if it returns false.

Can i use OnExecutePost() to do this? Can someone guide me on how to do this please?

dotty
  • 40,405
  • 66
  • 150
  • 195

2 Answers2

2

Yes. onPostExecute() is perfect for this. However, note that this method will not be called if your task is canceled, which is a case which you might want to handle similarly to the boolean returning false in a normal workflow.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • Is onPostExecute run on the UI thread? Could i simply do `intent = new Intent(main.this, newIntent.class);` then `startActivity(intent);`? – dotty Jul 01 '12 at 22:53
  • I can't seem to get `startActivity()` to run in `onPostExecute()`. – dotty Jul 01 '12 at 23:02
  • See http://stackoverflow.com/a/9118319/1069068 for launching an Activity from inside an AsyncTask. – Raghav Sood Jul 01 '12 at 23:04
0

You can use onPostExecute for it but to handle the case when your task is called use onCancelled to take care of that case

Mouna Cheikhna
  • 38,870
  • 10
  • 48
  • 69