How use ProgressDialog and Async Task get Method simultaneously?

I'm doing stuff like this all the time in my apps and the easiest way I found was to create "Callback" interface and pass it as a parameter to my "AsyncTask"s. You do your "doInBackground()" processing and when it's finished you call the "Callback" instance from onPostExecute passing the "result" object as parameter.

I'm doing stuff like this all the time in my apps and the easiest way I found was to create "Callback" interface and pass it as a parameter to my "AsyncTask"s. You do your "doInBackground()" processing and when it's finished you call the "Callback" instance from onPostExecute passing the "result" object as parameter. Below is a very simplified version of it.

Example of Callback interface: package example. App; public interface Callback { void run(Object result); } Example of AsyncTask using the Callback interface above: public class GetDataFromNetwork extends AsyncTask { Callback callback; public GetDataFromNetwork(Callback callback){ this. Callback = callback; } protected void onPreExecute() { super.onPreExecute(); progressDialog.show(); } protected Object doInBackground(Void... params) { Object result = null; // do your stuff here return result; } protected void onPostExecute(Object result) { callback.

Run(result); progressDialog.dismiss(); } } Example of how to use the classes above in your app: class Example { public onCreate(Bundle savedInstanceState){ //initialize GetDataFromNetwork request = new GetDataFromNetwork(new Callback(){ public void run(Object result){ //do something here with the result }}); request.execute(); } }.

Can you please explain in more detail . – Mohit Sharma Nov 9 at 9:07 I'll add some more details to my answer as it's not really gonna fit here. – DallaRosa Nov 9 at 9:13 Can you please mail me if possible for you , my id is mohit.1.

Sharma@silverskills. Com – Mohit Sharma Nov 9 at 9:20 Or any tutorial link – Mohit Sharma Nov 9 at 9:26 I'll try to write a tutorial on how to do this with some real code example when I have time, but for now I just posted here some simple example code. Hope it helps you.

Try adapting that to your code and let me know if it worked :) – DallaRosa Nov 9 at 10:08.

Why don't create another class in which you will put response and in there you will have get and set method. Then in onPostExecute you write it with set method and call a handler where you will do whatever you want...This is just an idea... :).

Thanks Jovan . I already tried that but in that case there is no synchronicity in my code – Mohit Sharma Nov 9 at 8:42.

If you are using the AsyncTask, your UI thread won't be blocked. Are you downloading your data inside doInBackground()? Because that is where you should be doing it.

Yes , I am doing pls see the code(just added that also) . Problem is with .get() Method in activity class. – Mohit Sharma Nov 9 at 8:28 cant understand – Mohit Sharma Nov 9 at 8:52 Why don't you use a Handler mechanism?

When the object is downloaded, send a message to the UI thread. – Reno Nov 9 at 9:09.

Like yorkw says, The problem is that you make the UI thread wait for the response with this code: response= (SoapObject)request.execute().get(); As the documentation for AsyncTask.get() says: Waits if necessary for the computation to complete, and then retrieves its result. Since the UI thread waits for the response, it can't show a progress dialog. The solution is to move the code that handles the response to the onPostExecute() method: protected void onPostExecute(Object result) { super.

OnPostExecute(result); // Now we have the response, dismiss the dialog and handle the response progressDialog.dismiss(); response = (SoapObject) result; } This method will be invoked after you have the response. Meanwhile, the UI thread can take care of showing a progress dialog.

Cant have generic processor for response . Secondly , how can I send response processed to my parent activity . – Mohit Sharma Nov 9 at 8:50 Then make the code in onPostExecute() depend on what object you received and/or the parameters you configured GetDataFromNetwork with.To return some result to the parent activity, use the usual Activity.

StartActivityForResult() and Activity.setResult() pattern. It is fine to call Activity.setResult() and Activity.finish() from onPostExecute(). – Martin Nordholts Nov 9 at 9:17 1 use AsyncTask.

OnPostExecute(SoapObject result), 2 the inner class AsyncTask inside your parent activity takes care of everything for you, do your task in background (separate thread), once done, go back and do whatever you want on UI thread. That is why it called AsyncTask, by calling AsyncTask.get() you make it synchronously (ui thread wait doInBackground thread). – yorkw Nov 9 at 9:31.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions