一次打开底部工作表对话框

时间:2020-02-24 07:47:02

标签: android dialog fragment

我的“底部表单”对话框有问题。如何使我的底页对话框仅打开一次?如果我在活动中两次单击该按钮,它将两次打开对话框。如何从活动中检查对话框是否打开?如果对话框打开,则单击按钮将关闭该对话框。这是我的代码,用于打开活动的片段。谢谢

            Bundle bundle = new Bundle();
            bundle.putString("outletData", new Gson().toJson(outletData));

            BookOutletDialogFragment bookOutletDialogFragment = BookOutletDialogFragment.newInstance();
            bookOutletDialogFragment.setArguments(bundle);
            bookOutletDialogFragment.show(getSupportFragmentManager(), bookOutletDialogFragment.TAG);

1 个答案:

答案 0 :(得分:1)

将“活动”中的标志设置为:-

    public static boolean isFragmentShown=false;

将片段的onPause()和onStart()方法修改为:-

  @Override
    public void onStart() {
        super.onStart();
       Log.e( "Fragment is visible", "Fragment is visible");
       **YouActivityName**.isFragmentShown = true;
    }

@Override
    public void onPause() {
        super.onPause();
        Log.e("Fragment is not visible", "Fragment is not visible");
        **YouActivityName**.isFragmentShown = false;
    }

然后在click方法中,将此代码添加到顶部以检查片段是否可见:-

        if(!isFragmentShown){
        Bundle bundle = new Bundle();
        bundle.putString("outletData", new Gson().toJson(outletData));

        BookOutletDialogFragment bookOutletDialogFragment = BookOutletDialogFragment.newInstance();
        bookOutletDialogFragment.setArguments(bundle);
        bookOutletDialogFragment.show(getSupportFragmentManager(), 
        bookOutletDialogFragment.TAG);}