执行代码时有时不显示对话框片段

Posted

技术标签:

【中文标题】执行代码时有时不显示对话框片段【英文标题】:Dialog Fragments not showing sometimes while the code is being executed 【发布时间】:2017-12-29 20:16:42 【问题描述】:

我在应用程序中有多个对话框片段。当屏幕变暗时,一次又一次没有显示对话框,就像出现对话框时一样。如果我从任务管理器中终止应用程序并再次打开,问题就解决了。有趣的是,在 android 7.0+ 上不会出现问题。我检查了日志,一切正常,正在执行代码。这是其中一个对话框片段:

/**
 * Created by yusufabd on 7/9/2017.
 */

public class DialogBankBranch extends DialogFragment implements View.OnClickListener, BankBranchView

    @BindView(R.id.text_bank_title)
    TextView tvTitle;

    @BindView(R.id.text_value_manager)
    TextView tvManager;

    @BindView(R.id.text_value_phone)
    TextView tvPhone;

    @BindView(R.id.text_value_email)
    TextView tvEmail;

    @BindView(R.id.text_value_info)
    TextView tvInfo;

    private static final String ARG_BANK = "arg_bank";

    private BankDatum bank;
    private BankBranchPresenter presenter;

    public static void showDialog(Activity activity, String title, String msg)

        DialogBankBranch dialog = new DialogBankBranch();

        Bundle bundle = new Bundle();
        dialog.setArguments(bundle);

        make(activity, dialog);

    

    public static void showDialog(Activity activity, BankDatum bank)

        DialogBankBranch dialog = new DialogBankBranch();

        Bundle bundle = new Bundle();
        bundle.putParcelable(ARG_BANK, bank);
        dialog.setArguments(bundle);

        make(activity, dialog);

    

    protected static void make(Activity activity, DialogFragment dialogFragment)
        if (activity != null)
            android.support.v4.app.FragmentTransaction transaction = ((FragmentActivity) activity).getSupportFragmentManager().beginTransaction();
            dialogFragment.show(transaction, "");
        
    

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) 
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        dialog.setCancelable(false);
        return dialog;
    


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        if (getArguments() != null)
            bank = getArguments().getParcelable(ARG_BANK);
        
        if (bank == null)
            showError(R.string.error_bank_data_not_found);
            getDialog().dismiss();
            return;
        
        presenter = new BankBranchPresenter(this, bank);

    

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        View view = inflater.inflate(R.layout.dialog_map, container);
        ButterKnife.bind(this, view);

        tvTitle.setText(bank.getBranch());
        tvManager.setText(bank.getManager());
        tvPhone.setText(bank.getPhone());
        tvEmail.setText(bank.getEmail());
        tvEmail.setOnClickListener(this);
        tvPhone.setOnClickListener(this);

        Spanned info = html.fromHtml(LanguageUtil.isUzbek() ? bank.getInfoUzb() : bank.getInfo());
        tvInfo.setText(info);

        return view;
    

    @Override
    public void onClick(View v) 
        presenter.onClick(v.getId());
    

    @Override
    public void start(Intent intent) 
        startActivity(intent);
    

    public void showError(String msg) 
        MyToast.show(msg);
    

    public void showError(int msgId) 
        MyToast.show(msgId);
    

没有错误时的样子:

这个错误:

这可能是什么原因?

【问题讨论】:

【参考方案1】:

因为您正在使用 fragmentManager 创建 dialogFragment,而您应该使用 fragmentChildManager 进行嵌套片段

【讨论】:

以上是关于执行代码时有时不显示对话框片段的主要内容,如果未能解决你的问题,请参考以下文章

片段中的请求权限不显示对话框

我该如何做模态对话框片段(代码在我关闭之前不会执行)

一个具有两种显示类型的片段[关闭]

如何在 BottomSheetDialogFragment 顶部显示另一个片段

附加到片段的片段对话框不调用 onYes/No 方法

在片段中我想点击如何显示警告对话框