0

I have a multi Activity application with a quit/logout button on most of the screens.

On pressing the button it shows a confirmation dialog and then sends out a 'quit' broadcast.

I have a broadcast receiver on each of the activity that simply calls the finish() activity and in the OnDestroy I unregister the receiver. The code works well in normal usage.

The issue that I am facing is if an uncaught exception pops up and a Force close happens.

After I press OK the last activity shows up. When I press the Quit/LogOut button the broadcast does not seem to be picked up by other activities.

I usually have to exit each of the activity or in some cases the 2nd time I press the Quit button the broadcast propagates through.

I am aware that I can setup the Thread.setDefaultUncaughtExceptionHandler() but,

Is there any thing that I am missing out over here?

The logcat does not show up anything.

Sankar Ganesh PMP
  • 11,927
  • 11
  • 57
  • 90
Ravishankar V
  • 323
  • 1
  • 8
  • 2
    You should not have a quit button in your app. See http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html – Al Sutton Feb 17 '11 at 11:22
  • The application does need a logout button as it is requires the user to login to the system and he can choose when to logout. Being a financial app it is quite necessary. It is all nice that the engineers at Android do not want the quit button but in real world scenarios there is a need for it. – Ravishankar V Feb 17 '11 at 11:33
  • A Quit button is not the same as a logout button. Have a logout button by all means. Do the logout in onPause or onStop by all means. However, you do _not_ require a quit button. The application lifecycle provides the methods you require to handle this situation, you should use them. – RivieraKid Feb 17 '11 at 11:42
  • I understand. My issue is really about propagating(broadcasting) a message to other activities and the fact that an Uncaught exception seems to be disrupting the reception of broadcasts. – Ravishankar V Feb 17 '11 at 11:49
  • An uncaught exception means that something serious went wrong in your app - if you don't handle it yourself, then Android considers it serious enough that your app is unrecoverable and that is why it (or parts of it) is terminated with no further code being executed. You can use `startActivityForResult` and `onActivityResult` to send messages back to previous activities, but you should probably catch all your exceptions first. – RivieraKid Feb 17 '11 at 13:01
  • This behavior came up as a result of an Out Of Memory exception while handling bitmaps on certain devices. That's when I noticed this behavior. Anyway I will be be setting up Thread.setDefaultUncaughtExceptionHandler() to prevent these issues. – Ravishankar V Feb 20 '11 at 19:12

1 Answers1

2

I have a broadcast receiver on each of the activity that simply calls the finish() activity and in the OnDestroy I unregister the receiver. The code works well in normal usage.

No, it doesn't.

Your approach assumes that all of the activities are in memory. That is not guaranteed to be the case. They may be part of the task but not in RAM, with their state held via onSaveInstanceState(). As a result, they will not receive your broadcast.

When the user chooses the "logout" menu choice or action bar entry, you should clear your authentication credentials (presumably held in a static data member or custom Application object), then start up whatever activity allows the user to log in. All of your activities should check, in onResume(), whether you have valid authentication credentials -- if not, then start up whatever activity allows the user to log in.

Among other things, this eliminates the broadcast that is causing you difficulty.

I am aware that I can setup the Thread.setDefaultUncaughtExceptionHandler() but,

You should have done this before you wrote your first activity.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491