Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

AsyncTask: Do background tasks and modify the GUI

Author: Roberto Calvo Palomino
Published? false -- FormatLanguage: W

Problem:

If you run a thread to execute some source code, you can't use or change any graphical widget from this thread and you don't know when the thread has finished.

Solution:

You can use AsyncTasks to develop and run background tasks and modify the GUI from this AsyncTask. It's often used to show progress dialog when the data is downloaded

Discussion:

(NOT FINISHED YET)


private class getDataTask extends AsyncTask<Void, Void, Void> {

        ProgressDialog pd;

        // In this method you can modify any graphical widget        
        protected void onPreExecute() {
            pd = ProgressDialog.show(Main.this,"Please wait...", "Retrieving data ...", true);         
        }
         
        @Override
        protected Void doInBackground(Void... params) {
               

        }
        
        // In this method you can modify any graphical widget
        protected void onPostExecute(Void unused) {
            if (!isFinishing()) 
            {
                pd.dismiss();
            }
        }       
    }

See Also:

http://developer.android.com/reference/android/os/AsyncTask.html

No records found.