0
任何你曾經遇到沒有被發現將其添加到片段Manager之後片段後立即發現了什麼?當我們試圖隱藏它時,它會停留在屏幕上。FragmentDialog無法通過標籤將其添加到getSupportFragmentManager
從片段:onActivityCreated我們顯示對話框:
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
// Push the progress dialog
String text = getActivity().getString(R.string.httpLoadingData);
((BaseFragmentActivity) getActivity()).showHttpWaitingDialog(text);
...
}
來自同一個片段之後一個新的線程裏面我們隱藏對話框:
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();
}
這裏是我們的BaseFragmentActivity發現代碼既顯示又隱藏。請注意,可以多次調用showdialog,所以我們保留一個refcount。
首先展示功能:
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");
}
}
然後隱藏功能:
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!");
}
}
}
}
}
我裹了進去\t \t runOnUiThread()的顯示和隱藏功能和它的工作。謝謝一堆! –