5

I am a beginner to Android and I have some confusions regarding Android UI Thread. Now, I know that no thread apart from the one that created the UI can modify it.

Great.
Here is the Activity from my first Android app which slightly confuses me.

public class NasaDailyImage extends Activity{
    public ProgressDialog modalDialog = null;
//------------------------------------------------------------------------------
    @Override
    protected void onCreate(Bundle savedInstanceState){

        //Instantiate progress dialog, skipping details.

        Button b = //get reference to button
        b.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                modalDialog.show(); // show modal
                Toast.makeText(getApplicationContext(), "Getting feeds", 500).show();
                new AsyncRetriever().execute(new IotdHandler()); // Get the feeds !!
            }
        });
    }
//------------------------------------------------------------------------------
    public synchronized void resetDisplay(boolean parseErrorOccured, 
        boolean imageErrorOccured,
        IotdHandler newFeeds){
        if(parseErrorOccured || imageErrorOccured){
            // make a Toast
            // do not update display
        }else{
            // make a Toast
            // update display
            // based on new feed
        }
    }
//------------------------------------------------------------------------------
    class AsyncRetriever extends AsyncTask<IotdHandler,Void,IotdHandler>{

        @Override
        protected IotdHandler doInBackground(IotdHandler... arg0) {
            IotdHandler handler = arg0[0];
            handler.processFeed(); // get the RSS feed data !
            return handler;
        }
//------------------------------------------------------------------------------    
        @Override
        protected void onPostExecute(IotdHandler fromInBackground){
            resetDisplay( // call to update the display
            fromInBackground.errorOccured,
            fromInBackground.imageError,
            fromInBackground);
        }
//------------------------------------------------------------------------------


}  


1. onCreate is on the UI thread so I can do whatever I want but onClick is not. Why can I make a ProgressDialog and a Toast in that method? Why no error there?
2. The AsyncTask is subclass of the the NasaDailyImage. This means it can access all the methods of NasaDailyImage including resetDisplay() which updates the display. resetDisplay() is called in the onPostExecute which runs on a different thread from UI. So, why can I update the display there and yet get no errors ?
An SO User
  • 24,612
  • 35
  • 133
  • 221
  • `onClick` and `onPostExecute` both run on the UI thread. The only thing in your code that doesn't is `doInBackground`, which as its name suggests, is run on a background thread. – Geobits Jul 24 '13 at 19:10
  • @Geobits AFAIK, this is different from the old Java where the events were processed on a **different thread** right ? UI was one thread, the event processing was on the other – An SO User Jul 24 '13 at 19:10
  • Read through this: http://stackoverflow.com/a/3653478/752320 – Geobits Jul 24 '13 at 19:12
  • In Android everything runs in the UI Thread unless you create a thread or use a framework class that uses threads. – type-a1pha Jul 24 '13 at 19:13

2 Answers2

3
  1. onClick() is indeed on the UI thread. Most of what happens in an Activity happens on the UI thread.

  2. onPostExecte() (and its counterpart onPreExecute()) runs on the UI thread as well. The AsyncTask.onPostExecte() documentation clearly states this. AsyncTask was deliberately designed such that developers could update the UI before and after they do background work.

In general, your code will be running on the UI thread unless you explicitly tell it otherwise. Once you create AsyncTasks, Runnables, or Threads, you need to ensure you understand where your code is executing. In an Activity, it is typically safe to assume you are on the UI thread.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • AFAIK, this is different from the old Java where the events were processed on a different thread right ? UI was one thread, the event processing was on the other – An SO User Jul 24 '13 at 19:14
  • What do you mean by "the old Java" and "events?" If you read through the source for Android, all the `onClick()` "event" is is a method in an interface. Events are nothing truly special. – Bryan Herbst Jul 24 '13 at 19:16
  • I mean the `addActionListener` was processed on a different thread right ? – An SO User Jul 24 '13 at 19:16
  • 2
    `ActionListener` is a part of the Java AWT package. AWT, Swing, and similar common UI-related packages found in most desktop Java apps do not exist in Android. I am not very familiar with `ActionListener` or it's methods of handling events, but it is not used in Android. There is no `addActionListener()` in Android. – Bryan Herbst Jul 24 '13 at 19:20
0
  1. You are extending AsyncTask class , where async task class is calling its sequential method automatically. First onPreExecute then doBackground and finally onPost. If you want to change any ui change you can use onProgressUpdate method.

  2. To use your activity class simple call activityclass.this.resetDisplay(). Because inner class scope sometimes failed to integrate except global varible.

Thanks

Mahmudul
  • 470
  • 1
  • 4
  • 12