0

I'm working on an Android app and there I have one Activity called InitActivity, where I send a request to the server in order register the user and update some data after the registration is completed.

During this action I wanted to show a ProgressDialog as a feedback to the user.

I it is possible to use AsyncTask's onPreExecute and onPostExecute to initialize and show ProgressDialog, while the AsyncHttpClient does its work in the doInBackground method but is there any better solution for this?

Thanks for any suggestions!

morten.c
  • 3,414
  • 5
  • 40
  • 45
amsalk
  • 577
  • 1
  • 5
  • 23

1 Answers1

0

Yes, you definitely can do what you have just stated. An AsyncTask is used just for this purpose - to do stuff in the background. It is advisable to always try to lessen the load on the main UI thread in Android. Assuming your registration process is not intensive or time consuming, AsyncTask would be the way to go. According to Android's documentation, you should use AsyncTask for short background processes.

Just a point to note here would be that you would be making the call to display your progress dialog box in your publishProgress() method and not in postExecute() which triggers only after the doInBackground() is complete.

ucsunil
  • 7,378
  • 1
  • 27
  • 32
  • First of all, thank you for the suggestion! I tought to use onPreExecute() to initialize and show the ProgressDialog and onPostExecute() to dismiss it. At least I was working like that till now. Have You ever used AsyncHttpClient before? It also has bunch of methods such as, onStart(), oFinish() etc. Is it possible to use some of them to show and later dismiss the ProgressDialog? In that case it is not necessary to use the AsyncTask or am I wrong about it? – amsalk Mar 20 '14 at 16:19
  • I haven't used AsyncHttpClient before on Android. I've used it with normal java applications. I don't think you could access/dismiss the ProgressDialog (or anything else that is currently on the UI) from the AsyncHttpClient's methods because you would be initializing and using it within the doInBackGround which does not have any hooks into the UI thread while publishProgress(), preExecute() and postExecute() do. You cam implement logic within the doInBackground() with calls to publishProgress() based on calls to onStart() and onFinish() but I would say it might get complicated. – ucsunil Mar 20 '14 at 16:49
  • Thx! I'll implement it as I've mentioned on the beginning. – amsalk Mar 20 '14 at 19:07