0

I try to show a progress dialog upon showing a sliding drawer.

this is opening drawer event handler:

public void onDrawerOpened(View drawerView) { 
    progress = ProgressDialog.show(activity, "dialog title",
            "dialog message", true);

    openDrawer();
} 

Inside openDrawer() i call a function fillCommunityList() that i need to show the progress dialog while its execution

fillCommunityList() implementation is as the following:

private void fillCommunityList(){
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {

                // Here you should write your time consuming task...
                UIManager manager = new UIManager(activity);
                coms = manager.getCommunities();
                progress.dismiss();
                getOutTread = false;
            } catch (Exception e) {

            }
            getOutTread = false;
        }
    }).start();
    // to stop code till thread be finished
    while(getOutTread){ }

    SlidingMenuExpandableListAdapter adapter = new SlidingMenuExpandableListAdapter(this,
            navDrawerItems, coms, mDrawerList);
    mDrawerList.setAdapter(adapter);
}

Note: I put a thread just to make progress dialog works

My problem is the following two points:

1- progress dialog appears too late for sudden and then disappears

2- Thread takes alot of time in its execution (without thread fillCommunityList() takes around 10 seconds but with a thread it takes more than a minute)

Note: manager.getCommunities() has asyncTask in its implementation

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

The problem is the following line

 while(getOutTread){ }

this is call busy waiting. The UI Thread is busy looping and can't, at the same time, draw/update the ProgressDialog

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • thank you for reply. "fillCommunityList()" get some data, i use this data for population the sliding menu. so i need to stop performing the remaining code till finishing code in the thread. Ho to do that without using while(getOutTread){ } ? – Ahmed Hamouda May 19 '15 at 17:31
  • One solution is to use the delegate pattern. Like a listenrt. Check my answer here: http://stackoverflow.com/questions/16752073/how-do-i-return-a-boolean-from-asynctask . it is about async task but the same concept can be easily applied also to your situation – Blackbelt May 19 '15 at 17:52
  • more description please ! – Ahmed Hamouda May 19 '15 at 17:53
  • did you see the link? – Blackbelt May 19 '15 at 19:17