In onResume() I do:
registerReceiver(timeTickReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
and in onPause():
unregisterReceiver(timeTickReceiver);
I see “java.lang.IllegalArgumentException: Receiver not registered” reports in Android Developer Console (there are only 2 reports and my app has thousands of users). The exception is triggered by unregisterReceiver(). What can be happening? I don’t feel very confident to just surround it with try-catch.
We’ve seen this error when doing a long press on a particular screen, and then immediately doing two orientation changes (e.g. turning the device upside down).
The API docs for unregisterReceiver says:
It doesn’t say explicitly, but as you’ve seen, you hit
IllegalArgumentException: Receiver not registeredif it isn’t already registered.The workaround I’m using is to store my Receiver as a member field, and have it set to null whenever it is not registered, i.e. I initialize it to null, and then only set it when I register it. This might not be perfect, but it does solve my crashes!
From my onServiceConnected:
From my onServiceDisconnected: