使用片段时如何关闭对话框?

时间:2019-10-03 10:28:35

标签: android android-alertdialog

我有一个要关闭的警报对话框参考。我有一个问题,就是我不能只从对话框中取消对它的引用,这是我的Fragment中的代码,因此您可以理解为什么-


@OnClick(R.id.verification_button_got_it)
    void onBtnGotItClicked(View view) {
        if (!checkBoxAge.isChecked()) {
            checkBoxAge.setTextColor(ContextCompat.getColor(checkBoxAge.getContext(), R.color.accent_red));
            return;
        }
        showProgressDialog();
        if (getContext() instanceof VerificationPageListener) {
            ((VerificationPageListener) getContext()).onPageAgreed();
        }
    }

private void showProgressDialog(){
        if (mBuilder == null) {
            mBuilder = new AlertDialog.Builder(App.getAppContext());
        }
        mBuilder.setCancelable(false);
        mBuilder.setView(R.layout.custom_proggress_dialog);
        mDialog = mBuilder.create();
        mDialog.show();
    }

此时,您会看到“ onPageAgreed()”是将数据发送到服务器以验证设备IMEI以进行验证的位置,因此,如果我在那时关闭对话框,则该对话框将不会显示完全是因为这将是立即的。

正如我所说,这是一个位于活动顶部的Fragment,因此活动通过DB事物处理整个检查。这是处理数据库检查并将结果移至演示者的活动的代码-


@Override
  public void onPageAgreed() {
    // current page is accepted, move to next
    int cur = viewPager.getCurrentItem();
    if (cur == adapter.getCount() - 1) {
      // ask for permission
      requestPhoneState();
    } else {
      // move to next
      viewPager.setCurrentItem(cur + 1, true);
    }
  }

  private void requestPhoneState() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
        != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, RC_PERMISSION_READ_STATE);
    } else {
      // retrieve IMEI
      accessDeviceIdAndVerifyDB(this);
    }
  }

  @Override
  protected void onRequestPermissionsResultPostResume(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResultPostResume(requestCode, permissions, grantResults);
    if (requestCode == RC_PERMISSION_READ_STATE
        && permissions.length > 0 && StringUtils.equals(permissions[0], Manifest.permission.READ_PHONE_STATE)
        && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
      // granted, read IMEI
      accessDeviceIdAndVerifyDB(this);
    } else {
      // failed
      onErrorDeviceIdNotGrantedPermission();
    }
  }

  private void accessDeviceIdAndVerifyDB(Activity activity) {
    if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
      TelephonyManager telephonyManager = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);
      if (telephonyManager != null) {
        @SuppressLint("HardwareIds") String deviceId = telephonyManager.getDeviceId();
        if (StringUtils.isNotBlank(deviceId)) {
          checkDeviceIdWithDB(deviceId);
        }
      }
    }
  }

  private void checkDeviceIdWithDB(String deviceId) {
    presenter.onDeviceIdReceived(deviceId);
  }


似乎我陷入了一个循环,在这个循环中,我必须将对话框的引用移到几个类上才能取消它,这似乎真的很糟糕。如何更轻松地取消对话框?

编辑-

我已经在活动结束之前添加了此代码-

@Override
  public void sendDeviceIdResult(String deviceId, boolean isAlreadyExist) {

    int currentItem = viewPager.getCurrentItem();
    Fragment item = adapter.getItem(currentItem);
    if (item instanceof PhoneStateAndAgeVerificationFragment) {
      Dialog dialog = ((PhoneStateAndAgeVerificationFragment) item).getProgressDialog();
      if (dialog != null && dialog.isShowing()) {
        dialog.dismiss();
      }
    }

    Intent intent = new Intent();
    intent.putExtra(KEY_DEVICE_ID, deviceId);
    intent.putExtra(KEY_IS_SUCCESS, !isAlreadyExist);
    setResult(RESULT_OK, intent);
    finish();


  }

但是在这一点上,我的对话框始终为空,并且我无法确定此时是否该对话框始终为空并且检查是多余的,还是我做错了什么?

1 个答案:

答案 0 :(得分:1)

您可以使用界面从dialog中关闭frament中的activity

在活动中创建如下所示的界面。

public class YourActivity extends AppCompactActivity{

  public interface onSomeEventListener {
    public void closeDialog();
  }

  onSomeEventListener someEventListener;
  //.............

@Override
  public void onPageAgreed() {
    // current page is accepted, move to next
    int cur = viewPager.getCurrentItem();
    if (cur == adapter.getCount() - 1) {
      // ask for permission
      requestPhoneState();
    } else {
      // move to next
      viewPager.setCurrentItem(cur + 1, true);
    }

   //you can add below line in your activity from where you can close your dialog. 
   someEventListener.closeDialog();
  }

在您的fragment中,您必须像下面那样实现该接口

public class YourFragment extends Fragment implements onSomeEventListener{

  @Override
  public void closeDialog() {
      // here you can close your dialog
      if (mDialog != null && mDialog.isShowing()) {
        mDialog.dismiss();
      }
  }
}

更新

sendDeviceIdResult()中,您可以使用上面的界面(如下所示)简化代码。

@Override
  public void sendDeviceIdResult(String deviceId, boolean isAlreadyExist) {

    int currentItem = viewPager.getCurrentItem();
    Fragment item = adapter.getItem(currentItem);
    if (item instanceof PhoneStateAndAgeVerificationFragment) {
       someEventListener = item; // initialize your interface here instead of onCreate()
       someEventListener.closeDialog(); 
    }

    Intent intent = new Intent();
    intent.putExtra(KEY_DEVICE_ID, deviceId);
    intent.putExtra(KEY_IS_SUCCESS, !isAlreadyExist);
    setResult(RESULT_OK, intent);
    finish();


  }