Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's absolutely right that any possibly long running operations should be executed in separate threads. And the <code>AsyncTask</code> is a good way to solve this kind of problems, since it also gives you an easy way to synchronize your task with the UI thread. This is the answer to your first question.</p> <p>Now, concerning the UI thread updating to show your users that your application is not stuck. Since an <code>AsyncTask</code>'s <code>onPreExecute()</code> and <code>onPostExecute()</code> methods are running inside the UI thread, you can easily create, run and stop <code>ProgressDialog</code>s or <code>ProgressBar</code>s there. If you want to show the current progress of the task, you should call <code>publishProgress(int)</code> method inside the <code>doInBackground()</code>, and then make use of it inside the <code>AsyncTask</code>'s <code>onProgressUpdate()</code> method. There you can, for example, update your <code>ProgressDialog</code>.</p> <p>And to get the result out of your AsyncTask you can either call its <code>get()</code> method (this a synchronous call), or implement some kind of callback interface that will tell the activity that the task has finished.</p> <p>I hope the answer is clear enough, if no - feel free to ask more questions. Hope this helps.</p> <p><strong>EDIT</strong> </p> <p>Create an interface called, for example, <code>onFetchFinishedListener</code> with one method - <code>void onFetchFinished(String)</code>. Your activity, that starts the <code>AsyncTask</code>, must implement this interface. Now create a constructor inside your <code>AsyncTask</code> that takes an <code>OnFetchFinishedListener</code> object as an argument, and when instantiating the <code>AsyncTask</code> inside your activity send a reference to the <code>Activity</code> as the argument (since it implements <code>OnFetchFinishedListener</code>). Then when your task is finished inside <code>doInBackground()</code> call <code>onFetchFinished()</code> on the activity. Now inside the <code>onFetchFinished(String)</code> method of your <code>Activity</code> you can make use of the <code>String</code> (or another object) that's brought with the callback. Again, hope I was clear enough.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload