Search code examples
androidbroadcastreceiverandroid-broadcast

Responding to broadcast message from activity


I have an intent service that broadcasts a message based on user action and my activity is registered to receive the message. Now, after the activity receives the message, I want my activity to respond to it depending on the message passed.

I have the below questions: 1. Will the receiver be still called even if my activity is killed? I don't want this to happen but would like to know if this is possible 2. Should I check for the current state of the activity (like which life-cycle method is currently being executed) before updating the activity UI in response to the message? Are there any best practices for handling the same?

Any helpful insight is appreciated. Thanks in advance


Solution

    1. The Receiver should be a member of the Activity.
    2. Register @ onResume(), unRegister @ onPause(), or onStart()&onStop()

    Now to answer your questions:

    1. No, if u unregister it at the correct place in the lifecycle there is no reason that the receiver will be called.
    2. No, because if its called then u know that the activity is visible to the user. But u need to check the UI elements because their state could change.