无法解除Activity里面的progressDialog

时间:2017-02-19 18:05:28

标签: android progressdialog

抱歉,我知道这个问题已被问过好几次并且有很多答案,但没有一个能解决我的问题。 我正在调用Web服务并显示对话框,该工作正常。但是我无法解雇progressDialog。虽然相同的方法在片段内工作,但这次我在Activity中使用它。请指出我犯了错误的地方。

    public void signupServiceResponse(String phNum,String password){
    progressDialog = createProgressDialog(this, false);
    //progressDialog.show();
    final ContentServiceCall request = ServiceGenerator.createService(ContentServiceCall.class, "Empty");
    final Call<UserServiceResponse> call = request.signUp(Constants.WS_VERSION,Constants.LOCAL_EN,Constants.PLATFORM, phNum,password);

    call.enqueue(new Callback<UserServiceResponse>() {
        @Override
        public void onResponse(Call<UserServiceResponse> call, final Response<UserServiceResponse> response) {
            if(response!=null && response.isSuccessful())
            {
                if(response.body()!=null && response.body().getResponse()!=null)
                {
                    if(response.body().getResponse().getResponseCode()== Constants.RESPONSE_CODE_SUCCESS) {

                        if(response.body().getUser() != null && response.body().getUserSubscription()!= null && response.body().getUserSubscription() !=null) {

                            userEntity = response.body().getUser();
                            userProfileEntity = response.body().getUserProfile();
                            userSubscriptionEntity = response.body().getUserSubscription();
                            //insert in user table
                            int tableCode = 1; //table code 1 for user table
                            dbHelper.insertUserRegistration(userEntity, userProfileEntity, userSubscriptionEntity, tableCode);
                            dbHelper.close();
                            progressDialog.dismiss();
                            Intent i = new Intent(RegistrationActivity.this, ActivateAccountActivity.class);
                            startActivity(i);
                        }

                    } else if((response.body().getResponse().getResponseCode()== Constants.USERAlREADYEXIST_RESPONSE_CODE_SUCCESS)) {
                        // in case user data is cleared or app is reinstalled
                        boolean userCount = dbHelper.getUserCount();
                        if (userCount) {
                            Intent i = new Intent(RegistrationActivity.this, MainActivity.class);
                            startActivity(i);

                        } else if(!userCount){
                            // if user exist and data is cleared
                            userEntity = response.body().getUser();
                            userProfileEntity = response.body().getUserProfile();
                            userSubscriptionEntity = response.body().getUserSubscription();
                            int tableCode = 1;
                            dbHelper.insertUserRegistration(userEntity, userProfileEntity, userSubscriptionEntity, tableCode);
                            dbHelper.close();
                        }

                    } else if((response.body().getResponse().getResponseCode()== Constants.RESPONSE_CODE_PASSWORD_INCORRECT)){
                        Toast.makeText(RegistrationActivity.this,"Password incorrect",Toast.LENGTH_LONG).show();
                        btnForgetPassword.setVisibility(View.VISIBLE);
                        progressDialog.dismiss();
                    }

                    else {
                    }
                }
                else {
                    // leave it
                }
            }
            else
            {
                // Display proper message
                //Toast.makeText(getActivity(),getString(R.string.error_webservice_response),Toast.LENGTH_LONG).show();
            }
            progressDialog.dismiss();
        }

        @Override
        public void onFailure(Call<UserServiceResponse> call, Throwable t) {

            Log.e("Fail", "Failure");
            Log.e("ERROR", t.getMessage());
                progressDialog.dismiss();
              Toast.makeText(RegistrationActivity.this,getString(R.string.error_internet_connectivity),Toast.LENGTH_LONG).show();
        }
    });
}

public ProgressDialog createProgressDialog(Context mContext, Boolean cancelable) {
    final ProgressDialog dialog = new ProgressDialog(mContext);
    try {
        dialog.show();
    } catch (WindowManager.BadTokenException e) {

    }
    dialog.setCancelable(cancelable);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.setContentView(R.layout.dialog);

    return dialog;
}

2 个答案:

答案 0 :(得分:1)

dialog设为全局变量并在onCreate()中初始化(如果您在活动中)。

dialog = new ProgressDialog(mContext);

添加此方法。

public ProgressDialog dismiss() {
    if(dialog != null) {
        dialog.dismiss();
    }
}

最后,不要只是致电progressDialog.dismiss(),而是致电dismiss()

答案 1 :(得分:0)

你必须像这样检查/写作。

if(progressDialog!= null&amp;&amp;  progressDialog.isshowing){

   progressDialog.dismiss();

}

我正在使用它正在运作。