Said
Said

Reputation: 23

Android - Display progress bar when certain operation is done by another thread

How to block UI thread from doing anything and just displaying a progress bar until certain condition is met. For example I have a list initialized to null, Lets call it list. I start another thread in UI to initialize its value which is going to take around 10 seconds. How can I block UI thread displaying progress dialog until the list is initialized.

// Initially
List list = null;

// Display progress bar until certain condition is met
while(list == null){
    // Display progress dialog
}

I think AsyncTask will work if we ask that AsyncTask to update the list etc, what about another thread doing it. I have tried to simplify the problem.

Upvotes: 1

Views: 403

Answers (1)

archetipo
archetipo

Reputation: 579

you need to create a public method in mainActivity, named for examble yourcallback()

instead of instantiating all the objects in the OnCreate mainActivity, you can instantiate all objects in addition to quell ic you need for the progressbar in the callback method yourcallback()

your callback now you need to create an extenction of AsyncTask where you declare a base method constructor like this

     privte Activity mainObj;
     public TestAsyncTask(Context context, Activity mainact ) throws Exception {
       mainObj=mainact
       your code 
     }

when the task naturally terminate the class AsyncTask call onPostExecute method

     protected void onPostExecute(Long result) {
        mainObj.yourcallback();
  }

hope this help!

Upvotes: 1

Related Questions