0

I have a project I am working on where I need to improve my knowledge on Threads.

Scenario: I have an Activity which calls a method Which use uses a thread:

Object soapResponse =  soaphttp.fetchNextCatalogueRange(0, numberOfItems);

In the soaphttp class I have:

Thread soapThread = new Thread(new Runnable()
    {           
        private Object serverResponse = new Object();

        public void run()
        {

              // Do network stuff here


        }   
    });

    soapThread.start();
   try
    {
        // crude synchronisation
        soapThread.join();
    }

The problem Using join() blocks the UI thread. If I dont use join() I get null pointer exceptions (data sync errors)

The Challenge: In my activity I would like to do stuff on the UI thread while the soaphttp class is fetching data and then sync i.e tell the UI thread that the data is ready. for example display a progress bar .. which will terminate when the data has finished being fetched.

How can I do this without having to use AsyncTask ?

1 Answers1

0

At the very end of your thread's run() method, use one of the following:

  • the post() method of View class,

  • the runOnUiThread() method of Activity class

in order to refresh your UI in the UI thread.

You can use the same methods to somehow alter your UI at the start of the run() method (make same widgets disabled, show some kind of progress indicator...)

cichystefan
  • 328
  • 2
  • 10