无法关闭 Activity 内的 progressDialog
unable to dismiss progressDialog inside Activity
抱歉,我知道这个问题已经被问过好几次并且有很多答案,但是 none 解决了我的问题。
我正在调用 Web 服务并显示对话框,它工作正常。但我无法关闭 progressDialog。虽然同样的方法在 fragment 中起作用,但这次我在 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;
}
你必须这样检查/写。
if(progressDialog!=null &&
progressDialog.isshowing){
progressDialog.dismiss();
}
我正在使用并且它正在工作。
将您的 dialog
设为全局变量并在 onCreate()
中对其进行初始化(如果您在 Activity 中)。
dialog = new ProgressDialog(mContext);
添加这个方法。
public ProgressDialog dismiss() {
if(dialog != null) {
dialog.dismiss();
}
}
最后,不要调用 progressDialog.dismiss()
,只需调用 dismiss()
。
抱歉,我知道这个问题已经被问过好几次并且有很多答案,但是 none 解决了我的问题。 我正在调用 Web 服务并显示对话框,它工作正常。但我无法关闭 progressDialog。虽然同样的方法在 fragment 中起作用,但这次我在 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;
}
你必须这样检查/写。
if(progressDialog!=null && progressDialog.isshowing){
progressDialog.dismiss();
}
我正在使用并且它正在工作。
将您的 dialog
设为全局变量并在 onCreate()
中对其进行初始化(如果您在 Activity 中)。
dialog = new ProgressDialog(mContext);
添加这个方法。
public ProgressDialog dismiss() {
if(dialog != null) {
dialog.dismiss();
}
}
最后,不要调用 progressDialog.dismiss()
,只需调用 dismiss()
。