Skip to content
tezvyn:

Static vs. Dynamic BroadcastReceivers: Implications & Restrictions

Source: developer.android.comMediumHow cards are made

Static vs. Dynamic BroadcastReceivers: Implications & Restrictions

This tests your grasp of Android's background restrictions. Explain that static receivers live with the app but are restricted post-API 26, while dynamic receivers are tied to a component's lifecycle. Ignoring modern API restrictions is a major red flag.

What's really being asked

This question probes your understanding of Android's evolution towards stricter battery and resource management. The interviewer isn't just looking for definitions; they are testing if you write modern, efficient code. They want to see if you know the critical background execution restrictions introduced in Android 7.0 (Nougat) and solidified in Android 8.0 (Oreo), and can explain the 'why' behind them.

The full answer

First, the core definition: Static receivers are declared in the AndroidManifest.xml and can launch an app process to respond to a broadcast. Dynamic receivers are registered in code using a Context (like an Activity or Service) and are bound to that context's lifecycle.

Second, the lifecycle difference: A dynamic receiver is only active while its registering component is. For an Activity, this is typically between onResume and onPause. A static receiver, in theory, is always active after app installation.

Third, the critical restriction: For apps targeting Android 8.0 (API 26) or higher, the system imposes significant restrictions on manifest-declared receivers. Most implicit broadcasts (system-wide events not targeted at your app) will no longer be delivered to them. This is the key point.

Fourth, modern use cases and exceptions: Mention that explicit broadcasts (sent directly to your app's package) are still fine for static receivers. Also, name a few protected implicit broadcasts that are still delivered, such as ACTION_BOOT_COMPLETED. The modern pattern is to use dynamic receivers for UI-related events and WorkManager for most deferred background work.

The mistakes people make

Giving a textbook definition of static vs. dynamic without mentioning the API 26+ restrictions. This answer is nearly a decade out of date and a major red flag.

Incorrectly stating that static receivers are completely deprecated or that all implicit broadcasts are blocked. Nuance is key; knowing the exceptions like BOOT_COMPLETED is a senior-level signal.

Failing to connect the restrictions to the reason: battery life and system health. Senior engineers understand the user-impact of technical decisions.

Suggesting a static receiver for a common but now-restricted broadcast like CONNECTIVITY_ACTION without qualifying that this no longer works on modern Android.

What usually comes next

"If you can't use a static receiver for network changes, what should you use to schedule work for when the network is available?" (Correct answer: WorkManager with a NetworkType constraint.)

"What is an explicit broadcast and why are they exempt from these restrictions?" (Answer: It targets a specific app component, so the system knows the sender's intent is deliberate, not an opportunistic system-wide event.)

"What happens if a user force-stops your app?" (Answer: The app is put into a stopped state and will not receive any broadcasts, including static ones for BOOT_COMPLETED, until the user manually launches it again.)

A concrete example

A permitted static receiver use case is registering for ACTION_BOOT_COMPLETED in the manifest to schedule recurring tasks with WorkManager after a device reboot. A dynamic receiver is perfect for an Activity that needs to listen for a local broadcast, like ACTION_DOWNLOAD_COMPLETE, to update the UI. You would register it in onResume and unregister it in onPause to prevent memory leaks and ensure it only runs when the UI is visible.

Interview question

For an app targeting Android 10 (API 29), why would you choose a dynamic BroadcastReceiver over a static one to listen for network connectivity changes?

  • a.Because static receivers for most implicit broadcasts, like CONNECTIVITY_ACTION, are not delivered to apps targeting API 26+.Correct
  • b.Because dynamic receivers can receive broadcasts even when the app is closed, while static receivers cannot.
  • c.Because static receivers are deprecated and will cause the app to crash on modern Android versions.
  • d.Because dynamic receivers consume less memory and are always more performant than static receivers.
Why?

For apps targeting Android 8.0 (API 26) or higher, manifest-declared static receivers will not receive most implicit broadcasts, including CONNECTIVITY_ACTION. The most tempting distractor is A, but static receivers are not deprecated, just restricted.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles