manmal
manmal

Reputation: 3928

How to show a dialog for an exception while halting the rest of the program flow?

I have thought out an IMHO neat way of handling checked Exceptions, like so:

protected void handleException(Throwable e, int messageResourceId, ExceptionReaction reaction) {...}

which would be part of the app's BaseActivity (thus usable in all Activities). Within this method I intend to show a dialog with the given message string and an OK button, and afterwards do something to react individually to the Exception (restart the activity, log the user out, reset database,...). However, the dialog is started on the UI thread non-blockingly, and the Activities having called handleException(...) would just continue execution while the dialog is shown - which sucks, because something most probably has gone very wrong and e.g. an activity-restart might be needed.

Note that the UI thread can't be put to sleep or wait, since then the OK button in the dialog would not work at all.

Has anybody got an idea how to achieve this?

Upvotes: 0

Views: 185

Answers (2)

CommonsWare
CommonsWare

Reputation: 1006584

Has anybody got an idea how to achieve this?

Put your "do something to react individually to the Exception" in the dialog close-button handler, such as via a final Runnable.

Upvotes: 1

Ron
Ron

Reputation: 24233

Have you tried UncaughtExceptionHandler?

Refer this doc Thread.UncaughtExceptionHandler

Implement this interface and do the handling stuff in the callback uncaughtException(Thread thread, Throwable ex)

Upvotes: 0

Related Questions