Android Broadcast Receiver and Force Close

I have a multitask application with a quit / logout button on most screens.

When the button is clicked, a confirmation dialog is displayed and then a quit broadcast is sent.

I have a broadcast receiver for each activity that simply calls the finish () operation, and in OnDestroy I unregister the recipient. The code works well under normal use.

The problem I am facing is that an uncovered exception pops up and a force closure occurs.

After I click OK, the last action will appear. When I click the Quit / LogOut button, the translation does not seem to be picked up by other actions.

I usually have to exit each action, or in some cases the second time I press the "Exit" button, through which the broadcast is distributed.

I know that I can set Thread.setDefaultUncaughtExceptionHandler (), but

Are there any things that I am missing here?

The log code does not display anything.

0
android broadcastreceiver
Feb 17 '11 at 11:13
source share
1 answer

I have a broadcast receiver for each activity that simply calls the finish () operation, and in OnDestroy I unregister the recipient. The code works well under normal use.

No, it is not.

Your approach assumes that all actions are in memory. This is not guaranteed. They can be part of the task, but not in RAM, and their state is carried out through onSaveInstanceState() . As a result, they will not receive your broadcast.

When the user selects the "logout" menu item or action bar line, you must clear the credentials for authentication (presumably stored in a static data item or user application object), and then start any activity that allows the user to log in to. All your actions should check in onResume() if you have valid credentials - if not, then start any activity that allows the user to log in.

Among other things, this eliminates the broadcast, which causes you difficulties.

I know that I can set Thread.setDefaultUncaughtExceptionHandler (), but

You should have done this before writing your first action.

+2
Feb 17 '11 at 12:58
source share



All Articles