progress dialog not getting dismissed in a fragment

1k Views Asked by At

I have a fragment that adds a few dynamic ui based on number of categories. I need to show a progress dialog and dismiss it when UI has been generated. I am calling pd.show() and pd.hide() at the beginning and end of category fetching process and end of ui generation. still My progress dialog is not getting dismissed.

 @Override
public void onCreate(@Nullable Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setTrackedPageName(R.string.analytics_select_service);
    pd = new ProgressDialog(getContext());
    pd.setIndeterminate(true);
    pd.setMessage("Preparing information..");
    pd.setCancelable(false);
    pd.show();


}

@Override
public void onAttach(Context context)
{
    super.onAttach(context);
    App.from(context).inject(this);

    categoriesSubscription = null;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.fragment_service_selection, container, false);
    if (view != null)
    {
        ButterKnife.inject(this, view);

        // We're recreating the view - we must be at the top of the scrollview!
        isScrolledDown = false;
    }


    return view;
}


@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);

    if (Categories.hasChildren(category))
    {
        populateWithCategories(category.getChildren());
    }
    else
    {
        categoriesSubscription = categories.list()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .onErrorResumeNext(Observable.empty())
                .subscribe(this::populateWithCategories);
    }




}

@Override
public void onDetach()
{
    super.onDetach();
    if (categoriesSubscription != null && !categoriesSubscription.isUnsubscribed())
    {
        categoriesSubscription.unsubscribe();
    }
}

@Override
public String getTitle()
{
    return null;
}

@Override
public float getDesiredActionBarElevation()
{
    return isScrolledDown ? ActionBarModifyingContent.DEFAULT_ELEVATION : 0;
}

private void populateWithCategories(List<Category> categories)
{
    for (int i = 0; i < categories.size(); i++)
    {
        Category category = categories.get(i);
        if (Categories.isKnown(category)
                && Categories.isValid(category))
                //&& Categories.hasAllowedGenders(category))
        {
            addService(category, i);
        }
    }

    getActivity().runOnUiThread(new Runnable() {
        public void run() {
           pd.dismiss();
            pd.hide();
            pd.cancel();
        }
    });



}

Where should I call pd.dismiss or pd.hide or pd.cancel?

3

There are 3 best solutions below

2
On BEST ANSWER

As the subscription is observed on main thread, therefore you need not run it on UI thread separately. Check this:

private void populateWithCategories(List<Category> categories)
{
    for (int i = 0; i < categories.size(); i++)
    {
        Category category = categories.get(i);
        if (Categories.isKnown(category)
                && Categories.isValid(category))
                //&& Categories.hasAllowedGenders(category))
        {
            addService(category, i);
        }
    }

    pd.dismiss();

}

If this doesn't solve the issue, try using a Handler instead:

private void populateWithCategories(List<Category> categories)
{
    for (int i = 0; i < categories.size(); i++)
    {
        Category category = categories.get(i);
        if (Categories.isKnown(category)
                && Categories.isValid(category))
                //&& Categories.hasAllowedGenders(category))
        {
            addService(category, i);
        }
    }

    new Handler().post(new Runnable(){

        @Override
        public void run(){
            pd.dismiss();
        }
    });
}

Hope this helps.

1
On
private void populateWithCategories(List<Category> categories)
{
dismiss();

    for (int i = 0; i < categories.size(); i++)
    {
        Category category = categories.get(i);
        if (Categories.isKnown(category)
                && Categories.isValid(category))
                //&& Categories.hasAllowedGenders(category))
        {
            addService(category, i);
        }
    }
0
On

This is what worked for me:

@Override
public float getDesiredActionBarElevation()
{
    return isScrolledDown ? ActionBarModifyingContent.DEFAULT_ELEVATION : 0;
}

private void populateWithCategories(List<Category> categories)
{
    for (int i = 0; i < categories.size(); i++)
    {
        Category category = categories.get(i);
        if (Categories.isKnown(category)
                && Categories.isValid(category))
                //&& Categories.hasAllowedGenders(category))
        {
            addService(category, i);
        }
    }

    pd.dismiss();
    pd = null;