1

I currently have a navigation drawer in which each option does a call to a server to get data. This is done via a class I made that receives the type of call as a string and makes the request in an AsyncTask, returning the data retrieved.

The problem I'm facing is, due to latency the navigation drawer keeps open while it is doing the request and parsing the data, which confuses the user into thinking it crashed.

I wanted it to close and show a ProgressDialog while the operation doesn't finnish. My current code does not work, any idea why? Thanks in advance

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();

    this.mDrawer.closeDrawer(GravityCompat.START);
    this.mDialog.show();

    if (id == R.id.nav_data) {
        this.dataList = this.myGetterClass.getRequests("some/API/route");
        try {
            if (this.dataList.size() == 0) {
                setTitle("There's no data");
            } else {
                setTitle("Here's the data");
            }
        } catch (Exception e) {
            setTitle("Without data");
            Toast error = Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG);
            error.show();
        }
    }
    updateScreen(this.dataList);
    this.mDialog.dismiss();
    return true;
}
edwardffs
  • 412
  • 1
  • 5
  • 17
  • You could show a progress bar instead of a ProgressDialog. Put it on your layout, hide it when there is no content and show it when loading your data. – Daivid Oct 12 '17 at 13:44
  • ProgressDialogs are now being deprecated: https://developer.android.com/reference/android/app/ProgressDialog.html – Daivid Oct 12 '17 at 13:46

2 Answers2

2

for close the navigationDrawer this worked for me

  drawerLayout.closeDrawer(Gravity.START);
  drawerToggle.syncState();

To show the ProgressBar you can define it in the xml file and then show and hide it when you want

2

To close drawer you need to replace

 mDrawer.closeDrawer(GravityCompat.START);

with

mDrawerLayout.openDrawer(Gravity.LEFT); //herer the drawer gravity from where drawer open

To show progress dialog best approach is to put progress in asynktask

if (id == R.id.nav_data) {
   //call asynk task for getting server data
}else if(id == R.id.nav_data1){
 //call asynk task for getting server data
}     

check this for more info about AsynkTask and this

Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55