← All articlesMobile applications

How to replace sticky broadcasts and restore the screen’s current state

When replacing sticky broadcasts, it is important to preserve predictable screen behavior: the screen should read the current state both when first opened and after a period without an active receiver. This requires separating two tasks: announcing a change and retrieving the current value. We will replace one of an app’s own sticky broadcasts using an entirely fictional example with synthetic task statuses. We will verify the result against the value in the app’s data source and on the screen, rather than merely checking that a notification was sent.

Why sticky broadcasts should not store screen state

A sticky broadcast keeps the sent Intent cached after the broadcast completes. The system can deliver it again to a receiver registered later. If a screen obtains its initial value this way, opening it depends on a stored message. The replacement recommended by the documentation is a regular broadcast and a separate mechanism for reading the current value, such as a local database.

There is also an access limitation: sticky broadcasts cannot be restricted to receivers with specific permissions, and the package-name filter does not apply when a broadcast is redelivered from the sticky cache. Sticky broadcasts are therefore unsuitable for sensitive data. Specifying your own package does not resolve this behavior of cached broadcasts.

Separate the state source from the change signal

In our entirely fictional app, a task has the synthetic statuses ‘pending’, ‘running’, and ‘completed’. The current value is stored in a local source owned by the app. The proposed update sequence is to write the new status first, then send a regular change notification. We include neither the status itself nor sensitive data in the notification.

The screen reads the source whenever it opens or resubscribes, and after receiving a notification. This makes the responsibilities explicit: the notification prompts a fresh read, while the value displayed comes from the app’s data source. A signal that might be missed should not be the only way to learn about a new status.

Choose a mechanism and restrict receivers

First, establish whether the sender and the screen are in the same process. The documentation strongly discourages self-broadcasts for this kind of communication: they are inefficient and create unnecessary system overhead. For this version of the example, use in-process change observation while retaining a separate read of the current value.

If you use a regular broadcast, specify the app’s own package in the Intent. The documentation also allows receivers to be restricted using permissions. For a context-registered receiver that listens only to broadcasts from its own app, use RECEIVER_NOT_EXPORTED. A context-registered receiver receives messages while its registration context remains valid; unregister it when it is no longer needed or the context is no longer valid.

Test the first opening and updates while active

The following checks are defensive tests proposed by the author, using your own synthetic data, rather than an official Android procedure. Start with the first opening: save ‘pending’ in the local source without sending a broadcast beforehand. Open the screen, register the receiver, and perform a separate read. Acceptance criterion: the screen displays ‘pending’, and verification of the read path confirms that it accesses the source without obtaining the initial value from a stored Intent.

For a positive control, leave the receiver active. Write ‘running’, then send a regular change signal without the status. When handling the received notification, the screen should read the source again and display ‘running’. Record the source value, signal reception, and displayed result separately: sending a message alone does not confirm that the screen updated. Do not include a guaranteed delivery time in the acceptance criterion.

Miss a notification and reopen the screen

The main test reproduces a period without a subscriber. Close the screen and unregister the receiver. Write the status ‘completed’ to the source and send a regular notification while no receiver is registered. Then reopen the screen, register the receiver, and read the source independently of the notification.

Acceptance criterion: the screen displays ‘completed’ without resending the missed notification. If it retains the old value or waits for the next signal, the required state restoration has not been implemented. For the in-process version, repeat the same scenario using the selected observation mechanism. The test checks the current value after returning, rather than receipt of every intermediate event.

Verify the fix with a code review

The first priority is to remove the dependency on the sticky cache and ensure an independent read when the screen opens. Then check the notification contents, receiver boundaries, and registration management. The author’s code review complements observation of the screen: a correct value in one run does not establish where it was read from.

  • The path being replaced contains no sticky broadcast sends or state reads from the sticky cache.
  • The source is read when the screen opens, when it resubscribes, and when it handles a notification.
  • The notification contains only a change signal, with no status or sensitive data.
  • Regular broadcasts specify the app’s own package; the receiver for the app’s own broadcasts uses RECEIVER_NOT_EXPORTED.
  • Registration is paired with unregistration when the receiver is no longer needed.

What the result confirms and where its limits lie

Successful checks confirm a specific behavior in the fictional scenario: the screen restores the current state from the source after missing a notification. They do not guarantee delivery of every event or the security of the entire app. Broadcast delivery time is not guaranteed, so it cannot serve as a basis for promising a screen update within a fixed time.

This article is limited to change notifications and restoring the current value. Other Android mechanisms and server-side processes require separate consideration. If your product has a website or web component, you can start with a free audit of that part through CheckMyApp; the Android checks described here remain an app development task.

Sources and documentation

  1. Sticky broadcasts
  2. Broadcasts overview
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