在Activity的onCreate中调用show()时,Android DialogFragment不显示

时间:2014-03-24 17:26:20

标签: android android-activity android-dialogfragment dialogfragment

我有一个对话框片段,中间有一个简单的不确定进度条,用于显示网络活动:

public class NativeLoadingDialogFragment extends DialogFragment {

public NativeLoadingDialogFragment() {
    // Blank
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_Dialog);
    ProgressBar indeterminateProgressBar = new ProgressBar(getActivity());
    indeterminateProgressBar.setIndeterminate(true);

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCancelable(false);
    dialog.setCanceledOnTouchOutside(false);
    dialog.setContentView(indeterminateProgressBar);

    dialog.getWindow().setBackgroundDrawable(
            new ColorDrawable(android.graphics.Color.TRANSPARENT));

    return dialog;
}

public boolean isShowing() {
    return getDialog() != null;
}
}

我在我的应用程序中使用了对话框片段没有任何问题,当我调用dialog.show(getFragmentManager, null)时,它在很多地方显示没有问题,但是当我尝试在我的设置活动的onResume中调用它时它没有显示!

我有设置活动,启动系统设置以更改手机语言。一旦用户更改语言并恢复我的活动,我会检测语言是否已更改并进行网络呼叫:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    mLoading = new NativeLoadingDialogFragment();
    if (savedInstanceState != null) {
            if (savedInstanceState.containsKey(EXTRA_LANGUAGE)) {
                String language = savedInstanceState.getString(EXTRA_LANGUAGE);
                String currentLanguage = AppUtils.getDefaultLanguageCode(
                        SmartBankConstant.DEFAULT_LANGUAGE,
                        SmartBankConstant.SUPPORTED_LANGUAGES);
                if (!language.equals(currentLanguage)) {
                    updateLanguage(Language.stringToLanguage(currentLanguage));
                }
            }
        }
   }

   private void updateLanguage(Language newLanguage) {
    ....
    getSpiceManager().execute(new SetLanguageRequest(newLanguage),
            new SetLanguageRequestListener(this));

         mLoading.show(getFragmentManager(), null);
        getFragmentManager().executePendingTransactions();
    }

代码肯定会运行,但不会出现对话框!如果网络调用失败,我有一个重试选项,再次调用updateLanguage(Language newLanguage)方法,对话框实际显示那个时间!我做错了什么?

2 个答案:

答案 0 :(得分:0)

尝试这种方法。它会检查对话框是否已经显示,否则会显示它。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    mLoading = new NativeLoadingDialogFragment();
    if (savedInstanceState != null) {
        ...
    }

    mLoading.show(getFragmentManager(), null);
}

private void updateLanguage(Language newLanguage) {
   ...

   if (mLoading != null && !mLoading.isVisible()) {
       mLoading.show(getFragmentManager(), null);
       getFragmentManager().executePendingTransactions();
   }
}

答案 1 :(得分:0)

我不知道为什么,但是在下一个循环中运行片段事务有助于解决这个问题。

new Handler().post(new Runnable() {
    @Override public void run() {
        // for some reason this must be called in the next loop
        dialogFragment.show(getSupportFragmentManager(), tag);
    }
});