title: Android Progress Bar stops working in the middle
body: <p>I am working with ASYNTask. And I have used a custom progressbar. It stops working after sometime of spinning, and after that a blank(black colored) screen comes for 2 secs and then my UI for the next screen loads on.</p>
  
  <pre><code>public class LoginActivity extends Activity {
      /** Variable define here. */
      private EditText metLoginUserName, metLoginPassword;
      private Button mbtnLogin;
      private ImageView ivRegister;
      private String Host, username, password;
      private int Port;
      private UserChatActivity xmppClient;
      public static ArrayList&lt;String&gt; all_user = new ArrayList&lt;String&gt;();
      public static CustomProgressDialog mCustomProgressDialog;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.loginlayout);
  
          metLoginUserName = (EditText) this.findViewById(R.id.etLoginUserName);
          metLoginPassword = (EditText) this.findViewById(R.id.etLoginPassword);
          mbtnLogin = (Button) findViewById(R.id.btnLogin);
          ivRegister = (ImageView) findViewById(R.id.ivRegister);
  
          /** Set the hint in username and password edittext */
          metLoginUserName = CCMStaticMethod.setHintEditText(metLoginUserName,
                  getString(R.string.hint_username), true);
          metLoginPassword = CCMStaticMethod.setHintEditText(metLoginPassword,
                  getString(R.string.hint_password), true);
  
          /** Click on login button */
          mbtnLogin.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  /** Login Activity */
                  username = metLoginUserName.getText().toString();
                  password = metLoginPassword.getText().toString();
                  CCMStaticVariable.musername = username;
                  CCMStaticVariable.musername = password;
                  if(CCMStaticMethod.isInternetAvailable(LoginActivity.this)){
                      LoginUserTask loginUserTask = new LoginUserTask(v.getContext());
                      loginUserTask.execute();
                  }
              }
          });
  
          /** Click on forgot button */
          this.findViewById(R.id.ivForgot).setOnClickListener(
                  new OnClickListener() {
                      @Override
                      public void onClick(View v) {
                          startActivity(new Intent(LoginActivity.this,
                                  ForgotPassActivity.class));
                      }
                  });
  
          /** Click on register button */
          ivRegister.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  startActivity(new Intent(LoginActivity.this,
                          RegisterActivity.class));
              }
          });
      }
  
      public class LoginUserTask extends AsyncTask&lt;Void, Void, XMPPConnection&gt; {
          public LoginUserTask(Context context) {
              super();
              this.context = context;
          }
          private Context context;
  
          @Override
          protected void onPostExecute(XMPPConnection result) {
              if (result != null) {
                  /**Start services*/
                  startService(new Intent(LoginActivity.this, UpdaterService.class));
                  /**Call usermenu activity*/ 
                  finish();
                  startActivity(new Intent(LoginActivity.this,UserMenuActivity.class));
              } else {
                  DialogInterface.OnClickListener LoginUnSuccessOkAlertListener = new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int which) {
                      }
                  };
                  CCMStaticMethod.showAlert(context, "Login",
                          "Invalid Username or Password. Try Again !", R.drawable.unsuccess,
                          true, true, "Ok", LoginUnSuccessOkAlertListener, null,
                          null);
              }
              mCustomProgressDialog.dismiss();
          }
  
          @Override
          protected void onPreExecute() {
              mCustomProgressDialog = CustomProgressDialog.createDialog(
                      LoginActivity.this, "", "");
              mCustomProgressDialog.show();
          }
  
          @Override
          protected XMPPConnection doInBackground(Void... params) {
              CCMStaticVariable.CommonConnection = XMPPConn.checkXMPPConnection(username,
                      password);
              return CCMStaticVariable.CommonConnection;
          }
      }
  }
  </code></pre>
  
  <p>UserMenuActivity</p>
  
  <pre><code>public class UserMenuActivity extends ExpandableListActivity {
  
      private XMPPConnection connection;
      String name,availability,subscriptionStatus;
      TextView tv_Status;
      public static final String ACTION_UPDATE = "ACTION_UPDATE";
  
      /** Variable Define here */
      private String[] data = { "View my profile", "New Multiperson Chat",
              "New Broad Cast Message", "New Contact Category", "New Group",
              "Invite to CCM", "Search", "Expand All", "Settings", "Help",
      "Close" };
      private String[] data_Contact = { "Rename Category","Move Contact to Category", "View my profile",
              "New Multiperson Chat", "New Broad Cast Message",
              "New Contact Category", "New Group", "Invite to CCM", "Search",
              "Expand All", "Settings", "Help", "Close" };
  
      private String[] data_child_contact = { "Open chat", "Delete Contact","View my profile",
              "New Multiperson Chat", "New Broad Cast Message",
              "New Contact Category", "New Group", "Invite to CCM", "Search",
              "Expand All", "Settings", "Help", "Close" };
      private String[] menuItem = { "Chats", "Contacts", "CGM Groups", "Pending","Request" };
      private List&lt;String&gt; menuItemList = Arrays.asList(menuItem);
  
      private int commonGroupPosition = 0;
      private String etAlertVal;
      private DatabaseHelper dbHelper;
      private int categoryID, listPos;
      /** New Code here.. */
      private ArrayList&lt;String&gt; groupNames;
      private ArrayList&lt;ArrayList&lt;ChildItems&gt;&gt; childs;
      private UserMenuAdapter adapter;
      private Object object;
      private CustomProgressDialog mCustomProgressDialog;
  
      private String[] data2 = { "PIN Michelle", "IP Call" };
      private ListView mlist2;
      private ImageButton mimBtnMenu;
      private LinearLayout mllpopmenu;
      private View popupView;
      private PopupWindow popupWindow;
      private AlertDialog.Builder alert;
      private EditText input;
      private TextView mtvUserName, mtvUserTagLine;
      private ExpandableListView mExpandableListView;
      public static List&lt;CategoryDataClass&gt; categoryList;
      public static ArrayList&lt;String&gt; chatUser;
      private boolean menuType = false;
      private String childValContact="";
      public static Context context;
  
      @Override
      public void onCreate(Bundle icicle) {
          super.onCreate(icicle);
          setContentView(R.layout.usermenulayout);
          dbHelper = new DatabaseHelper(UserMenuActivity.this);
          XMPPConn.getContactList();
          connection = CCMStaticVariable.CommonConnection;
          registerReceiver(UpdateList, new IntentFilter(ACTION_UPDATE));
  
      }
  
      @Override
      public void onBackPressed() {
          if (mllpopmenu.getVisibility() == View.VISIBLE) {
              mllpopmenu.setVisibility(View.INVISIBLE);
          } else {
              if (CCMStaticVariable.CommonConnection.isConnected()) {
                  CCMStaticVariable.CommonConnection.disconnect();
              }
              super.onBackPressed();
          }
      }
  
      @SuppressWarnings("unchecked")
      @Override
      public boolean onKeyDown(int keyCode, KeyEvent event) {
          if (keyCode == KeyEvent.KEYCODE_MENU) {
              if (mllpopmenu.getVisibility() == View.VISIBLE) {
                  mllpopmenu.setVisibility(View.INVISIBLE);
              } else {
                  if (commonGroupPosition &gt;= 4 &amp;&amp; menuType == true) {
                      if(childValContact == ""){
                          mllpopmenu.setVisibility(View.VISIBLE);
  
                          mlist2.setAdapter(new ArrayAdapter(UserMenuActivity.this,
                                  R.layout.listviewtext, R.id.tvMenuText,
                                  data_Contact));
  
                      }else{
                          mllpopmenu.setVisibility(View.VISIBLE);
                          mlist2.setAdapter(new ArrayAdapter(UserMenuActivity.this,
                                  R.layout.listviewtext, R.id.tvMenuText,
                                  data_child_contact));
  
                      }
  
                  } else if (commonGroupPosition == 0) {
                      mllpopmenu.setVisibility(View.VISIBLE);
                      mlist2.setAdapter(new ArrayAdapter(UserMenuActivity.this,
                              R.layout.listviewtext, R.id.tvMenuText, data));
                  }
              }
              return true;
          }
          return super.onKeyDown(keyCode, event);
      }
  
  
  
      @Override
      protected void onResume() {
          super.onResume();
          mCustomProgressDialog = CustomProgressDialog.createDialog(
                  UserMenuActivity.this, "", "");
          mCustomProgressDialog.show();
          new Thread(){
              public void run() {
                  XMPPConn.getContactList();
                  expendableHandle.sendEmptyMessage(0);
              };
          }.start();
          super.onResume();
      }
  
      @Override
      protected void onDestroy() {
          // TODO Auto-generated method stub
          super.onDestroy();
  
          unregisterReceiver(UpdateList);
      }
  
      public boolean onChildClick(ExpandableListView parent, View v,
              int groupPosition, int childPosition, long id) {
          if(groupPosition == 0){
              Intent intent = new Intent(UserMenuActivity.this,UserChatActivity.class);
              intent.putExtra("userNameVal",chatUser.get(childPosition));
              intent.putExtra("message","");          
              startActivity(intent);
          }else if (groupPosition == 1 &amp;&amp; childPosition == 0) {
              startActivity(new Intent(UserMenuActivity.this,
                      InvitetoCCMActivity.class));
          } else if (groupPosition == 1 &amp;&amp; childPosition != 0) {
              Intent intent = new Intent(UserMenuActivity.this,
                      UserChatActivity.class);
              intent.putExtra("userNameVal",
                      XMPPConn.mfriendList.get(childPosition - 1).friendName);
              intent.putExtra("message","");
              startActivity(intent);
          } else if (groupPosition == 2 &amp;&amp; childPosition == 0) {
              startActivity(new Intent(UserMenuActivity.this,
                      CreateGroupActivity.class));
          } else if (groupPosition == 2 &amp;&amp; childPosition != 0) {
              String GROUP_NAME = childs.get(groupPosition).get(childPosition)
              .getName().toString();
              int end = GROUP_NAME.indexOf("(");
              CCMStaticVariable.groupName = GROUP_NAME.substring(0, end).trim();
              startActivity(new Intent(UserMenuActivity.this,
                      GroupsActivity.class));
          } else if (groupPosition &gt;= 4) {
              childValContact = childs.get(groupPosition).get(childPosition).getName().trim();
              showToast("user==&gt;"+childValContact, 0);
          }
          return false;
      }
  
      private void setExpandableListView() {
          /***###############GROUP ARRAY ############################*/
          final ArrayList&lt;String&gt; groupNames = new ArrayList&lt;String&gt;();
          chatUser = dbHelper.getChatUser();
          groupNames.add("Chats ("+chatUser.size()+")");  
          groupNames.add("Contacts (" + XMPPConn.mfriendList.size() + ")");
          groupNames.add("CGM Groups (" + XMPPConn.mGroupList.size() + ")");
          groupNames.add("Pending (1)");
  
          XMPPConn.getGroup();
          categoryList = dbHelper.getAllCategory();
          /**Group From Sever*/
          if (XMPPConn.mGroupList.size() &gt; 0) {
              for (int g = 0; g &lt; XMPPConn.mGroupList.size(); g++) {
                  XMPPConn.getGroupContact(XMPPConn.mGroupList.get(g).groupName);
                  groupNames.add(XMPPConn.mGroupList.get(g).groupName + "("
                          + XMPPConn.mGroupContactList.size()+ ")");
              }
          } 
          if(categoryList.size() &gt; 0){
              for (int cat = 0; cat &lt; categoryList.size(); cat++) {
                  groupNames.add(categoryList.get(cat).getCategoryName()+ "(0)");
              }
          }
          this.groupNames = groupNames;
  
          /*** ###########CHILD ARRAY * #################*/
          ArrayList&lt;ArrayList&lt;ChildItems&gt;&gt; childs = new ArrayList&lt;ArrayList&lt;ChildItems&gt;&gt;();
          ArrayList&lt;ChildItems&gt; child = new ArrayList&lt;ChildItems&gt;();
          if(chatUser.size() &gt; 0){
              for(int i = 0; i &lt; chatUser.size(); i++){
                  child.add(new ChildItems(userName(chatUser.get(i)), "",0,null));
              }
          }else{
              child.add(new ChildItems("--No History--", "",0,null));
          }       
          childs.add(child);
  
          child = new ArrayList&lt;ChildItems&gt;();
          child.add(new ChildItems("", "",0,null));
          if (XMPPConn.mfriendList.size() &gt; 0) {
              for (int n = 0; n &lt; XMPPConn.mfriendList.size(); n++) {
                  child.add(new ChildItems(XMPPConn.mfriendList.get(n).friendNickName,
                          XMPPConn.mfriendList.get(n).friendStatus, 
                          XMPPConn.mfriendList.get(n).friendState,
                          XMPPConn.mfriendList.get(n).friendPic));
              }
          }
          childs.add(child);
          /************** CGM Group Child here *********************/
          child = new ArrayList&lt;ChildItems&gt;();
          child.add(new ChildItems("", "",0,null));
  
          if (XMPPConn.mGroupList.size() &gt; 0) {
              for (int grop = 0; grop &lt; XMPPConn.mGroupList.size(); grop++) {
                  child.add(new ChildItems(
                          XMPPConn.mGroupList.get(grop).groupName + " ("
                          + XMPPConn.mGroupList.get(grop).groupUserCount
                          + ")", "",0,null));
              }
          }
          childs.add(child);
  
          child = new ArrayList&lt;ChildItems&gt;();
          child.add(new ChildItems("Shuchi",
                  "Pending (Waiting for Authorization)",0,null));
          childs.add(child);
          /************************ Group Contact List *************************/
          if (XMPPConn.mGroupList.size() &gt; 0) {
              for (int g = 0; g &lt; XMPPConn.mGroupList.size(); g++) {
                  /** Contact List */
                  XMPPConn.getGroupContact(XMPPConn.mGroupList.get(g).groupName);
                  child = new ArrayList&lt;ChildItems&gt;();
                  for (int con = 0; con &lt; XMPPConn.mGroupContactList.size(); con++) {
                      child.add(new ChildItems(
                              XMPPConn.mGroupContactList.get(con).friendName,
                              XMPPConn.mGroupContactList.get(con).friendStatus,0,null));
                  }
                  childs.add(child);
              }
          } 
          if(categoryList.size() &gt; 0){
              for (int cat = 0; cat &lt; categoryList.size(); cat++) {
                  child = new ArrayList&lt;ChildItems&gt;();
                  child.add(new ChildItems("-none-", "",0,null));
                  childs.add(child);
              }
          }
          this.childs = childs;
  
          /** Set Adapter here */
          adapter = new UserMenuAdapter(this, groupNames, childs);
          setListAdapter(adapter);
          object = this;
  
          mlist2 = (ListView) findViewById(R.id.list2);
          mimBtnMenu = (ImageButton) findViewById(R.id.imBtnMenu);
          mllpopmenu = (LinearLayout) findViewById(R.id.llpopmenu);
          mtvUserName = (TextView) findViewById(R.id.tvUserName);
          mtvUserTagLine = (TextView) findViewById(R.id.tvUserTagLine);
  
          //Set User name..
          System.out.println("CCMStaticVariable.loginUserName==="
                  + CCMStaticVariable.loginUserName);
          if (!CCMStaticVariable.loginUserName.equalsIgnoreCase("")) {
              mtvUserName.setText("" + CCMStaticVariable.loginUserName);
          }
          /** Expandable List set here.. */
          mExpandableListView = (ExpandableListView) this
          .findViewById(android.R.id.list);
          mExpandableListView.setOnGroupClickListener(new OnGroupClickListener() {
              @Override
              public boolean onGroupClick(ExpandableListView parent, View v,
                      int groupPosition, long id) {   
  
                  if (parent.isGroupExpanded(groupPosition)) {
                      commonGroupPosition = 0;
                  }else{
                      commonGroupPosition = groupPosition;
                  }
                  String GROUP_NAME = groupNames.get(groupPosition);
                  int end = groupNames.get(groupPosition).indexOf("(");
                  String GROUP_NAME_VALUE = GROUP_NAME.substring(0, end).trim();
                  if (menuItemList.contains(GROUP_NAME_VALUE)) {
                      menuType = false;
                      CCMStaticVariable.groupCatName = GROUP_NAME_VALUE;
                  } else {
                      menuType = true;
                      CCMStaticVariable.groupCatName = GROUP_NAME_VALUE;
                  }
                  long findCatId = dbHelper.getCategoryID(GROUP_NAME_VALUE);
                  if (findCatId != 0) {
                      categoryID = (int) findCatId;
                  }
                  childValContact="";
                  //showToast("Clicked on==" + GROUP_NAME_VALUE, 0);
                  return false;
              }
          });
  
          /** Click on item */
          mlist2.setOnItemClickListener(new OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int pos,long arg3) {
                  if (commonGroupPosition &gt;= 4) {
                      if(childValContact == ""){
                          if (pos == 0) {
                              showAlertEdit(CCMStaticVariable.groupCatName);
                          }                   
                          /** Move contact to catgory */
                          if (pos == 1) {
                              startActivity(new Intent(UserMenuActivity.this,AddContactCategoryActivity.class));
                          }
                      }else{
                          if(pos == 0){
                              Intent intent = new Intent(UserMenuActivity.this,UserChatActivity.class);
                              intent.putExtra("userNameVal",childValContact);
                              startActivity(intent);
                          }
                          if(pos == 1){
                              XMPPConn.removeEntry(childValContact);
                              showToast("Contact deleted sucessfully", 0);
                              Intent intent = new Intent(UserMenuActivity.this,UserMenuActivity.class);
                          }
                      }
  
                  } else {
                      /** MyProfile */
                      if (pos == 0) {
                          startActivity(new Intent(UserMenuActivity.this,
                                  MyProfileActivity.class));
                      }
                      /** New multiperson chat start */
                      if (pos == 1) {
                          startActivity(new Intent(UserMenuActivity.this,
                                  NewMultipersonChatActivity.class));
                      }
                      /** New Broadcast message */
                      if (pos == 2) {
                          startActivity(new Intent(UserMenuActivity.this,
                                  NewBroadcastMessageActivity.class));
                      }
                      /** Click on add category */
                      if (pos == 3) {
                          showAlertAdd();
                      }
                      if (pos == 4) {
                          startActivity(new Intent(UserMenuActivity.this,
                                  CreateGroupActivity.class));
                      }
  
                      if (pos == 5) {
                          startActivity(new Intent(UserMenuActivity.this,
                                  InvitetoCCMActivity.class));
                      }
                      if (pos == 6) {
                          startActivity(new Intent(UserMenuActivity.this,
                                  SearchActivity.class));
                      }
                      if (pos == 7) {
                          onGroupExpand(2);
                          for (int i = 0; i &lt; groupNames.size(); i++) {
                              mExpandableListView.expandGroup(i);
                          }
                      }
                      /** Click on settings */
                      if (pos == 8) {
                          startActivity(new Intent(UserMenuActivity.this,
                                  SettingsActivity.class));
                      }
                      if (pos == 10) {
                          System.exit(0);
                      }
  
                      if (pos == 14) {
                          if (mllpopmenu.getVisibility() == View.VISIBLE) {
                              mllpopmenu.setVisibility(View.INVISIBLE);
                              if (popupWindow.isShowing()) {
                                  popupWindow.dismiss();
                              }
                          } else {
                              mllpopmenu.setVisibility(View.VISIBLE);
  
  
                              mlist2.setAdapter(new ArrayAdapter(
                                      UserMenuActivity.this,
                                      R.layout.listviewtext, R.id.tvMenuText,
                                      data));
                          }
                      }
                  }
              }
          });
      }
  
      /** Toast message display here.. */
      private void showToast(String msg, int time) {
          Toast.makeText(this, msg, time).show();
      }
  
      /** Show EditAlert Box */
      private void showAlertEdit(String msg) {
          alert = new AlertDialog.Builder(UserMenuActivity.this);
          input = new EditText(UserMenuActivity.this);
          input.setSingleLine();
          input.setText(msg);
          alert.setTitle("Edit Category Name");
          alert.setView(input);
          alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {
                  dialog.cancel();
                  etAlertVal = input.getText().toString();
                  /** isGroupFromServerOrMobile */
                  RosterGroup isGroupExists = CCMStaticVariable.CommonConnection
                  .getRoster().getGroup(CCMStaticVariable.groupCatName);
                  if (isGroupExists == null) {
                      dbHelper.updateCategory(categoryID, etAlertVal);
                      Intent intent = new Intent(UserMenuActivity.this,
                              UserMenuActivity.class);
                      startActivity(intent);
                      showToast("Category updated sucessfully", 0);
                  } else {
                      CCMStaticVariable.CommonConnection.getRoster()
                      .getGroup(CCMStaticVariable.groupCatName)
                      .setName(etAlertVal);
                      Intent intent = new Intent(UserMenuActivity.this,
                              UserMenuActivity.class);
                      startActivity(intent);
                      showToast("Category updated sucessfully", 0);
                  }
              }
          });
  
          alert.setNegativeButton("Cancel",
                  new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {
                  dialog.cancel();
              }
          });
          alert.show();
      }
  
      /** ShowAlertBox Edit */
      private void showAlertAdd() {
          alert = new AlertDialog.Builder(UserMenuActivity.this);
          input = new EditText(UserMenuActivity.this);
          input.setSingleLine();
          alert.setTitle("New Category Name");
          alert.setView(input);
          alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {
                  dialog.cancel();
                  etAlertVal = input.getText().toString();
                  if (!etAlertVal.equalsIgnoreCase("request")) {
                      long lastInsertedId = dbHelper.insertCategory(etAlertVal);
                      CCMStaticVariable.groupCatName = etAlertVal;
                      Intent intent = new Intent(UserMenuActivity.this,AddContactCategoryActivity.class);                 
                      startActivity(intent);
                  }
              }
          });
          alert.setNegativeButton("Cancel",
                  new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {
                  dialog.cancel();
              }
          });
          alert.show();
      }
  
  
  
      public String showSubscriptionStatus(String friend){
          return friend;
      }
      public String userName(String jid){
          String username = "";
          if(jid.contains("@")){
              int index = jid.indexOf("@");
              username = jid.substring(0, index);
          }else{
              username = jid;
          }       
          return username;
      }
  
      /**Set expandable handler here..*/
      Handler expendableHandle = new Handler(){
          public void handleMessage(android.os.Message msg) {
              mCustomProgressDialog.dismiss();
              setExpandableListView();
          };
      };
  
      BroadcastReceiver UpdateList = new BroadcastReceiver() {
  
          @Override
          public void onReceive(Context context, Intent intent) {
              // TODO Auto-generated method stub
              Toast.makeText(UserMenuActivity.this, "CALLED", Toast.LENGTH_SHORT).show();
              adapter.notifyDataSetChanged();
  
  
          }
      };
  
  
  }
  </code></pre>
  
  <p>Please suggest me what is the problem here. I have seen so many posts and have tried a lot of things regarding the same. But It has not worked for me. Please tell me.</p>
  
  <p>Thanks</p>