Skip to content

Commit

Permalink
Ignore exceptions from dismissing dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumBadger committed Mar 28, 2017
1 parent bb5a220 commit 38a3b66
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(final DialogInterface dialogInterface) {
General.quickToast(CommentEditActivity.this, R.string.comment_reply_oncancel);
progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
}
});

Expand All @@ -112,7 +112,7 @@ public boolean onKey(final DialogInterface dialogInterface, final int keyCode, f

if(keyCode == KeyEvent.KEYCODE_BACK) {
General.quickToast(CommentEditActivity.this, R.string.comment_reply_oncancel);
progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
}

return true;
Expand All @@ -125,7 +125,7 @@ protected void onSuccess() {
AndroidApi.UI_THREAD_HANDLER.post(new Runnable() {
@Override
public void run() {
if(progressDialog.isShowing()) progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
General.quickToast(CommentEditActivity.this, R.string.comment_edit_done);
finish();
}
Expand All @@ -146,7 +146,7 @@ protected void onFailure(@CacheRequest.RequestFailureType int type, Throwable t,
@Override
public void run() {
General.showResultDialog(CommentEditActivity.this, error);
if(progressDialog.isShowing()) progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
}
});
}
Expand All @@ -160,7 +160,7 @@ protected void onFailure(final APIFailureType type) {
@Override
public void run() {
General.showResultDialog(CommentEditActivity.this, error);
if(progressDialog.isShowing()) progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(final DialogInterface dialogInterface) {
General.quickToast(CommentReplyActivity.this, getString(R.string.comment_reply_oncancel));
progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
}
});

Expand All @@ -188,7 +188,7 @@ public boolean onKey(final DialogInterface dialogInterface, final int keyCode, f

if(keyCode == KeyEvent.KEYCODE_BACK) {
General.quickToast(CommentReplyActivity.this, getString(R.string.comment_reply_oncancel));
progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
}

return true;
Expand All @@ -201,7 +201,7 @@ protected void onSuccess() {
AndroidApi.UI_THREAD_HANDLER.post(new Runnable() {
@Override
public void run() {
if(progressDialog.isShowing()) progressDialog.dismiss();
General.safeDismissDialog(progressDialog);

if(mParentType == ParentType.MESSAGE) {
General.quickToast(CommentReplyActivity.this, getString(R.string.pm_reply_done));
Expand Down Expand Up @@ -230,7 +230,7 @@ protected void onFailure(@CacheRequest.RequestFailureType int type, Throwable t,
@Override
public void run() {
General.showResultDialog(CommentReplyActivity.this, error);
if(progressDialog.isShowing()) progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
}
});
}
Expand All @@ -244,7 +244,7 @@ protected void onFailure(final APIFailureType type) {
@Override
public void run() {
General.showResultDialog(CommentReplyActivity.this, error);
if(progressDialog.isShowing()) progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(final DialogInterface dialogInterface) {
General.quickToast(PMSendActivity.this, getString(R.string.comment_reply_oncancel));
progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
}
});

Expand All @@ -183,7 +183,7 @@ public boolean onKey(final DialogInterface dialogInterface, final int keyCode, f

if(keyCode == KeyEvent.KEYCODE_BACK) {
General.quickToast(PMSendActivity.this, getString(R.string.comment_reply_oncancel));
progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
}

return true;
Expand All @@ -197,7 +197,7 @@ protected void onSuccess() {
@Override
public void run() {

if(progressDialog.isShowing()) progressDialog.dismiss();
General.safeDismissDialog(progressDialog);

mSendSuccess = true;

Expand Down Expand Up @@ -225,7 +225,7 @@ protected void onFailure(@CacheRequest.RequestFailureType int type, Throwable t,
@Override
public void run() {
General.showResultDialog(PMSendActivity.this, error);
if(progressDialog.isShowing()) progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
}
});
}
Expand All @@ -239,7 +239,7 @@ protected void onFailure(final APIFailureType type) {
@Override
public void run() {
General.showResultDialog(PMSendActivity.this, error);
if(progressDialog.isShowing()) progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public boolean onOptionsItemSelected(final MenuItem item) {
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(final DialogInterface dialogInterface) {
General.quickToast(PostSubmitActivity.this, getString(R.string.comment_reply_oncancel));
progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
}
});

Expand All @@ -220,7 +220,7 @@ public boolean onKey(final DialogInterface dialogInterface, final int keyCode, f

if(keyCode == KeyEvent.KEYCODE_BACK) {
General.quickToast(PostSubmitActivity.this, getString(R.string.comment_reply_oncancel));
progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
}

return true;
Expand All @@ -235,7 +235,7 @@ protected void onSuccess() {
AndroidApi.UI_THREAD_HANDLER.post(new Runnable() {
@Override
public void run() {
if(progressDialog.isShowing()) progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
General.quickToast(PostSubmitActivity.this, getString(R.string.post_submit_done));
finish();
}
Expand All @@ -256,7 +256,7 @@ protected void onFailure(@CacheRequest.RequestFailureType int type, Throwable t,
@Override
public void run() {
General.showResultDialog(PostSubmitActivity.this, error);
if(progressDialog.isShowing()) progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
}
});
}
Expand All @@ -270,7 +270,7 @@ protected void onFailure(final APIFailureType type) {
@Override
public void run() {
General.showResultDialog(PostSubmitActivity.this, error);
if(progressDialog.isShowing()) progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
}
});
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/quantumbadger/redreader/common/General.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.quantumbadger.redreader.common;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
Expand Down Expand Up @@ -617,4 +618,12 @@ public static void recreateActivityNoAnimation(final AppCompatActivity activity)
public static long hoursToMs(final long hours) {
return hours * 60L * 60L * 1000L;
}

public static void safeDismissDialog(final Dialog dialog) {
try {
if(dialog.isShowing()) dialog.dismiss();
} catch(final Exception e) {
Log.e("safeDismissDialog", "Caught exception while dismissing dialog", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void onActivityResult(final int requestCode, final int resultCode, final
@Override
public void onCancel(final DialogInterface dialogInterface) {
cancelled.set(true);
progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
}
});

Expand All @@ -78,7 +78,7 @@ public void onCancel(final DialogInterface dialogInterface) {
public boolean onKey(final DialogInterface dialogInterface, final int keyCode, final KeyEvent keyEvent) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
cancelled.set(true);
progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
}
return true;
}
Expand All @@ -96,7 +96,7 @@ public void onLoginSuccess(final RedditAccount account) {
AndroidApi.UI_THREAD_HANDLER.post(new Runnable() {
@Override
public void run() {
progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
if (cancelled.get()) return;

final AlertDialog.Builder alertBuilder = new AlertDialog.Builder(mActivity);
Expand All @@ -121,7 +121,7 @@ public void onLoginFailure(final RedditOAuth.LoginError error, final RRError det
AndroidApi.UI_THREAD_HANDLER.post(new Runnable() {
@Override
public void run() {
progressDialog.dismiss();
General.safeDismissDialog(progressDialog);
if (!cancelled.get()) General.showResultDialog(mActivity, details);
}
});
Expand Down

0 comments on commit 38a3b66

Please sign in to comment.