Interview frequency: …
Short answer
A statically registered one is declared in the manifest and the app is woken up to receive it, while a dynamic one is registered programmatically in code and lives with the lifecycle of the component that registered it.
Explanation
Modern Android versions (starting with Android 8.0) restrict most implicit broadcasts in the manifest to limit battery drain and unnecessary waking of apps. So most sensitive events (such as a change in network connectivity) are registered dynamically through registerReceiver in onStart and unregistered in onStop to avoid leaks.
Code example
val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
// Perform quick processing
}
}
override fun onStart() {
super.onStart()
registerReceiver(receiver, IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED))
}
override fun onStop() {
super.onStop()
unregisterReceiver(receiver)
}Common mistakes
- Forgetting to call unregisterReceiver when the component is destroyed, leaking the Context and crashing on repetition.
- Performing complex operations or long network calls inside onReceive, which is limited to a very short response time.