1

[Edit] Just to make it shorter. I know how to update progressDialog from within the doInBackground. I'm looking for a way to update the progress from a different class while the Async class stays in the main activity. [Edit]

In my main activity I have several buttons, each executing a different AsyncTask. Because I want to keep my code nice I want to perform the doInBackground stuff in a different class, something like this:

Main activity

 @Override
    protected ArrayList<Object> doInBackground(String... params) {
        String query = params[0];
        return OtherClass.performSomethingThatTakesTime(query);
    }

    @Override
    protected void onPostExecute(final Object result) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                sendIntentToAnotherClass(result);
            }
        });
    }

At this timeI have the entire OtherClass.performSomethingThatTakesTime(query) inside the doInBackground and I update the progress through the code using publishProgress(value). But like I said I'd prefer to have the code in a different class but can I update the progress on UI from a different class?

Paveliko
  • 53
  • 2
  • 8
  • 1
    Maybe you can pass `this` as a `performSomethingThathTakesTime` parameter, and invoke "publishProgress()" inside your class. – manelizzard Oct 21 '12 at 18:22
  • I don't know how passing `this` will allow me to invoke `publishProgress()` inside different class. Can you explain with some code? – Paveliko Oct 21 '12 at 20:23
  • Sorry, it was an "on the fly" chance. I tried to implement it, but the method `onProgressUpdate` is protected, so you can not call it outside its class (or outside derived classes) – manelizzard Oct 22 '12 at 07:16
  • [Here](http://stackoverflow.com/a/5518007/276311) you can find the solution. – Adriano P Nov 11 '12 at 01:26

2 Answers2

0

Pass the ProgressDialogs object to the AsyncTask and update it in one of the methods, that is run on the UI thread.

You can create your own custom constructor for your AsyncTask and then hold the ProgressDialog in a global variable.

See here for which methods that runs on the UI thread: http://developer.android.com/reference/android/os/AsyncTask.html - look under the different methods (onPreExecute, onPostExecute, publishProgress and so on.).

Darwind
  • 7,284
  • 3
  • 49
  • 48
  • I'm not sure I understand what do you mean. Right now the progress dialog is defined in the `onPreExecute` and is updated in the `onProgressUpdate`. I want to accomplish what I posted in the main question (without methods running on the UI thread). – Paveliko Oct 21 '12 at 18:42
  • The different methods of the AsyncTask is running on the UI thread per default. Hence you won't need to use runOnUiThread or a Handler to call the ProgressDialog. So basically when you create your class extending the AsyncTask class, you should create a custom constructor and pass the reference to the ProgressDialog in it. Look at this pastebin code - I haven't tested the code, so don't expect it to compile or anything - it's for reference only. http://pastebin.com/8DujUJ6n – Darwind Oct 21 '12 at 18:51
  • If I correctly understand (correct me if I'm wrong) what you wrote and the code you provided this works if I create the whole AcyncTask in a new external class. But I can't do it this way because I have to have onPostExecute in the main thread. – Paveliko Oct 21 '12 at 20:12
  • Ok so the question changed a bit I see. So you want to keep the AsyncTask as an inner class, but move the logic out of the class and then call back to the progressUpdate? The different methods of the AsyncTask will always run on the main (GUI) thread. Please explain why you want to do it like this - what's the motivation? You might be going about some common problem in a wrongly manner. As I mentioned earlier, the methods of an AsyncTask will always run on the main thread of the application even though it's not an inner class of an Activity. – Darwind Oct 22 '12 at 19:11
  • Just wanted to point out for anyone who may be confused by some of the mistaken assertions in this thread. The whole POINT of AsyncTask is that most of its methods do NOT run in the main (GUI) thread. A few methods, like onProgressUpdate() but NOT publishProgress(), need to be run on the UI thread. Those methods are not invoked by you. They will be called on your behalf. – Peter vdL Aug 07 '14 at 22:04
0

You can find your answer here.. hope it will help

http://www.codingforandroid.com/2011/06/basic-asynctask-with-progress-bar.html

also see- http://mobileorchard.com/android-app-developmentthreading-part-2-async-tasks/

also see- Android : AsyncTask, how can update ProgressDialog increment

Community
  • 1
  • 1
Swati
  • 1,179
  • 9
  • 28
  • Sorry, but all the links talk about updating the progressDialog from within the doInBackground.. This is not what I'm looking for. – Paveliko Oct 22 '12 at 13:49