ProgressDialog not stopping if I click twice on menu/button

Viewed 104

I am using ProgressDialog to show loading whenever user clicks on any menu, and it works fine. But when the user clicks twice on menus, ProgressDialog not stopping, it's running endlessly.

How can I solve this issue?

This is my code structure:

async void Grid_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
    _pbr = Utilities.ProgressBar(this, "b").Show();
   switch (e.Position)
   {
    case 0:
        var result = await objHw.GetHomework(userid,log.LogInOrganizationID);
        if (resultA.Any())
        {
            //code
        }
        else
        {
            _pbr.Hide();
            Toast.MakeText(this, "Homework not available.", ToastLength.Short).Show();
        }
        _pbr.Hide();
        break;
    case 1: 
    case 2:
  }
}

Utilities.ProgressBar method:

public static ProgressDialog ProgressBar(Context context, string position)
{
    var pbr = new ProgressDialog(context);
    pbr.SetCancelable(false);
    pbr.Indeterminate = true;
    pbr.SetProgressStyle(ProgressDialogStyle.Spinner);
    switch (position.ToLower())
    {
        case "c":
            pbr.Window.SetGravity(GravityFlags.Center);
            break;
        case "b":
            pbr.Window.SetGravity(GravityFlags.Bottom);
            break;
    }
    pbr.SetMessage("please wait..");
    return pbr;
}

Loading...

enter image description here

1 Answers

ProgressDialog not stopping if I click twice on menu/button

When user clicks twice on menus, avoid to create multiple instance for your ProgressDialog should resolve this issue:

public class Utilities
{
    public static ProgressDialog pbr;
    public static ProgressDialog ProgressBar(Context context, string position)
    {
        if(pbr == null)
        {
            pbr = new ProgressDialog(context);
        }

        pbr.SetCancelable(false);
        ...
        pbr.SetMessage("please wait..");
        return pbr;
    }
}
Related