Skip to content

Commit

Permalink
Fix possible crash in SafeBroadcastReceiver
Browse files Browse the repository at this point in the history
  • Loading branch information
Nain57 committed Jan 11, 2025
1 parent efcac3f commit ddc75ef
Showing 1 changed file with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ abstract class SafeBroadcastReceiver(private val intentFilter: IntentFilter) : B
get() = registrationContext != null

fun register(context: Context, exported: Boolean = true) {
if (isRegistered) return

registrationContext = context
ContextCompat.registerReceiver(
context,
Expand All @@ -43,11 +41,13 @@ abstract class SafeBroadcastReceiver(private val intentFilter: IntentFilter) : B
}

fun unregister() {
if (!isRegistered) return
try {
registrationContext?.unregisterReceiver(this)
} catch (iaEx: IllegalArgumentException) {
return
}

registrationContext!!.unregisterReceiver(this)
registrationContext = null

onUnregistered()
}

Expand Down

0 comments on commit ddc75ef

Please sign in to comment.