Android: Leaked IntentReceiver exception is being thrown even though I call unregisterReceiver

前端 未结 3 1405
轻奢々
轻奢々 2020-12-20 11:47

I don\'t understand why I\'m getting this exception when hitting the back button. I have the IntentReceiver registered in the onCreate method and it is supposed to be unregi

相关标签:
3条回答
  • 2020-12-20 12:12

    I had the exact same problem. The cause was that I had inadvertently registered the same BroadcastReceiver twice.

    0 讨论(0)
  • 2020-12-20 12:23

    This is a Android activity life cycle issue. I am seeing it in a main activity and then testing on device with the back button that goes back to a splash screen.

    In the onPause() method.

    Unregister the BroadcastReceiver that you created in the onCreate()

    In the onRestart() re-register a brand new Broadcast Receiver.

    In the activity class you need to keep a record of the Broadcast Receiver as instance data member.

    SECOND

    I think this also a feature enhancement issue with Android.

    Sometimes developer need a broadcast receiver to outlive the activity. For example to understand when certain screen states are available or not. Think about a conversation context of work flow model, which has many states.

    THIRD

    You can register and unregister broadcast receivers with an activity, but a simple call like isRegistered(BroadcastReceiver) in the Activity class might be very useful.

    If you need receivers to live beyond the activity, then I do not the answer, except to silence the warning, by adding unregister(X) in the onDestroy() call. YMMV ;-)

    0 讨论(0)
  • 2020-12-20 12:29

    My situation was similar with Mullins, I registered a receiver in both a class and its subclass.

    For newbies like me, just toggle a breakpoint at your receiver, then debug your application. If the receiver is something like com.example.main.listener.MyRecevier@421c0100, while the error log in logcat is different from it, such as com.example.main.listener.MyRecevier@4202fb40, with characters after "@" different, surely you registered it twice (or more).

    (Above likes a comment under Mullins' answer, actually, I post it here because I can't post comments now.)

    0 讨论(0)
提交回复
热议问题