开发者

Android Broadcast Receiver and Force Close (Uncaught exceptions)

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 cas开发者_运维技巧es 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜