← All articlesMobile applications

How to Scope BroadcastReceiver Registration to a Screen’s Lifecycle and Verify Unregistration

Define the active period of a BroadcastReceiver as a testable rule: the receiver is registered when the screen needs events and unregistered when it leaves the chosen state. We will examine a rule for a single Activity: one context-registered receiver is active only between onResume and onPause. Using an entirely fictional example, we will explore which logs and processing results can confirm this rule when opening, leaving, and returning to the screen.

Define the active scope first

A context-registered receiver receives broadcasts while its registration context is valid; this period is typically bounded by calls to registerReceiver and unregisterReceiver. Choose the smallest scope in which the application actually needs the result. In our example, this is the resumed state of a particular Activity, rather than the entire lifetime of the application.

LifecycleResumeEffect or the onResume/onPause pair can define this boundary. We use the second option below: register when entering resumed and unregister when leaving it. If navigation works differently and a screen does not correspond to a single Activity, define its active boundaries separately: this rule cannot automatically be applied to every screen structure.

Pair registration with unregistration

Establish two operations in the implementation: register the selected receiver in onResume, and call unregisterReceiver in onPause for the same instance through the same context. The result to verify is one active registration within the chosen period and no outstanding registrations after leaving it. Simply having both methods in the code does not show that the required calls completed during a particular transition.

A registered receiver holds a reference to its Context. If the registration scope extends beyond that context’s lifecycle, a leak is possible. Unregistration must therefore match the point when the receiver is no longer needed or the context is no longer valid. Checking paired calls helps assess this contract, but does not replace an investigation of memory across the application.

Define the trust boundary for your own events

In this example, the receiver listens only for broadcasts from its own application, so registration uses RECEIVER_NOT_EXPORTED. Verify that this flag is passed separately from checking the lifecycle: registration duration and the set of senders are different implementation conditions. Intentionally exported receivers without appropriate access controls may be used by applications the developer did not intend to grant access to.

Our entirely fictional “Training Requests” screen uses only synthetic data we own. A test sender within the application sends an event with the action example.training.REQUEST_CHANGED and a unique identifier. The expected processing is to record that identifier in a test log of processed events. No third-party applications or real user data are involved in the check.

Check opening the screen and the positive control

The following checks are an original proposed test, not an official Android procedure. Add Activity and receiver identifiers, lifecycle transitions, successfully completed registration and unregistration operations, and processed event identifiers to the test instrumentation. Distinguish the intention to call a method from confirmation that the call completed.

Open the screen and confirm in the log that it entered resumed and completed one registration. Then send your own event with a new identifier. Acceptance criterion: the selected receiver received this identifier, and test processing recorded it exactly once. A record of sending alone does not confirm processing. Broadcast delivery timing is not guaranteed; if the positive control is not observed, the delivery check remains inconclusive.

Check the transition out of resumed

Move the Activity out of resumed. First, find a completed unregisterReceiver call in the log for the previously registered instance through the same context. Only then send a new synthetic event with a different identifier. Check the test-run log for an absence of processing of this event outside the chosen scope.

Assess the result together with confirmed unregistration and a successful positive control. Silence from the receiver during an arbitrarily chosen pause does not, by itself, prove unregistration: delivery timing is not guaranteed. The criterion for this stage combines the completed matching unregistration operation with no observed processing of the new event outside the defined period.

Repeat entry and eliminate accumulated registrations

Repeat opening, leaving, and reopening the screen, including creating a new Activity instance. Using successful operations, match each completed active period to a registration and unregistration pair. After leaving, there should be no outstanding registrations; on the next entry, there should be only one active registration for the selected receiver. Repeat the positive control with a new identifier and check that it is processed once.

When making fixes, first restore the chosen onResume/onPause boundary and the pairing of operations. Then eliminate extra registrations found during repeated entries, and verify RECEIVER_NOT_EXPORTED separately. After the changes, repeat the entire scenario. For internal notification that an event has completed, you can also consider a callback: the security guidance suggests this as a replacement for a broadcast receiver.

What the check confirms and where its limits lie

The test run shows whether the defined registration contract was met during the transitions checked: receipt of your own event within the scope, matching unregistration on exit, and no accumulation of registrations when reopening the screen. It does not establish a guarantee for all executions or prove that the entire application is free of memory leaks.

This article does not cover recovering missed state, replacing sticky broadcasts, goAsync, configuring manifest components, or permissions between applications. For a website or the web portion of a product, you can separately take advantage of a free CheckMyApp audit.

Sources and documentation

  1. Broadcasts overview
  2. Insecure broadcast receivers
CheckMyApp

Check your application’s security.

Enter your web application address and email. Get a free preliminary report and choose the depth of analysis.

Check for free