0

Any of you ever experience a fragment not been found after adding it to the fragment manager? When we try to hide it, it stays stuck on screen.

From fragment:onActivityCreated we show the dialog:

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

    // Push the progress dialog
    String text = getActivity().getString(R.string.httpLoadingData);
    ((BaseFragmentActivity) getActivity()).showHttpWaitingDialog(text);

    ... 
}

Later from the same fragment inside a new thread We hide the dialog:

private void prepareInitialWebViewData() {
    initialFragmentWebDataLoadingThread = new Thread(new Runnable() {

        @Override
        public void run() {
            updateDataAndView();

            BaseFragmentActivity activity = (BaseFragmentActivity) getActivity();

                    BaseFragmentActivity activity = (BaseFragmentActivity) getActivity();
                    if (activity != null) 
                    {
                        activity.hideHttpWaitingDialog();
                    }

            // We don't need to keep this handle any longer since we've done
            // the work
            initialFragmentWebDataLoadingThread = null;
        }
    });

    initialFragmentWebDataLoadingThread.start();
}

Here is the code found in our BaseFragmentActivity for both show and hide. Note that it is possible to call the showdialog many times, so we keep a refcount.

First the show function:

public void showHttpWaitingDialog(CharSequence title)
{
    synchronized (mRefCount)
    {
        mRefCount++;

        Log.w("showhideHttpWaitingDialog", "++mRefCount:" + mRefCount + ", Title:" + title);

        FragmentManager fm = getSupportFragmentManager();
        if (fm != null)
        {
            Fragment frag = fm.findFragmentByTag("httpWaitDialog");

            if (frag == null)
            {
                WaitingOnHttpFragmentDialog dialog = WaitingOnHttpFragmentDialog.newInstance(title);

                fm.beginTransaction().add(dialog, "httpWaitDialog").commit();
            }
        }
        else
            Log.w("showhideHttpWaitingDialog", "fragman == null");
    }
}

Then the hide function:

public void hideHttpWaitingDialog()
{
    synchronized (mRefCount)
    {
        Log.w("showhideHttpWaitingDialog", "--mRefCount:" + mRefCount);

        if (mRefCount < 0)
        {
            Log.w("showhideHttpWaitingDialog", "Why are you trying to hide something that doesn't exists?");
            mRefCount = 0;
        }
        else
        {
            if (mRefCount == 0)
            {
                FragmentManager fragman = getSupportFragmentManager();
                if (fragman != null)
                {
                    Fragment frag = fragman.findFragmentByTag("httpWaitDialog");

                    if (frag != null)
                    {
                        fragman.beginTransaction().remove(frag).commit();
                        Log.w("showhideHttpWaitingDialog", "dismissed normally");
                    }
                    else
                        Log.w("showhideHttpWaitingDialog", "httpWaitDialog not found!");
                }
            }
        }
    }
}
Richard Lalancette
  • 2,401
  • 24
  • 29

1 Answers1

1

I can tell you what your problem is... you can't update the UI in any other thread than the UI thread.

I don't know how to fix it myself, but a quick search reveals the following questions which may be useful.

SO Question 1

SO Question 2

The second one seems like it would apply more closely to your current code.

Community
  • 1
  • 1
Barak
  • 16,318
  • 9
  • 52
  • 84