3

I have an application that contains LoginActivity which makes WebService request to authorize user. If credentials are correct, I store in Auth singleton its session ID.

However during application usage, user can be signed out (server restart, etc). In this case I can throw some AuthException. It can be thrown in every application's Activity.

How to catch AuthException in every Activity to finish current one and start LoginActivity again ?

Is this exception reachable in Application or some AuthLayerActivity that others will extend ?

hsz
  • 148,279
  • 62
  • 259
  • 315
  • Yeah you'll want to have an `AuthLayerActivity` route your call's through this, then this can finish the activity on the exception and start your login. You won't catch it in `Application` – Blundell Aug 30 '12 at 14:24

4 Answers4

1

I wouldn't use Exceptions to do that. Especially if you try to propagate the Exception from one Activity to another.

But activities usually need to update their state from time to time and if you check the authentication state then you can go back to login if required. Alternatively sending a broadcast would work as notification as well.

zapl
  • 63,179
  • 10
  • 123
  • 154
0

You may want to declare a default uncaught exception handler with Thread.setDefaultUncaughtExceptionHandler()

See Ideal way to set global uncaught exception Handler in Android:

Community
  • 1
  • 1
sdabet
  • 18,360
  • 11
  • 89
  • 158
0

You can use a parent activity inherited by all your activities that will handle the exception part.

njzk2
  • 38,969
  • 7
  • 69
  • 107
0

you can use my fancy FitActivity which is able to handle all exceptions and do something depends on provided strategy. Look here: https://github.com/rysiekblah/android-commons/blob/master/src/main/java/com/rysiekblah/android/commons/activity/FitActivity.java

Tomek Kozlowski
  • 604
  • 7
  • 6