I have a custom Chronometer which extends TextView and works as a stopwatch. The Activity that uses this Chronometer is listening for broadcasts from a Service. In this broadcast the Service tells the Activity (and thus the Chronometer) to stop counting. This works fine when keeping the application on foreground.
However, when the Activity is not active (like the user presses the home button), the Activity doesn’t get the broadcast, and the Chronometer keeps counting. So when I open the Activity again, the Chronometer is still counting up, when it shouldn’t be.
Who can provide me with a solution for this?
Note: the Service does not have any knowledge of the Activity nor the Chronometer. It just sends a broadcast “Hey, I’m done with whatever I was doing!”.
Your service should be called with
startForeground(int id, Notification notification);See why…
By the way, your
BroadcastReceivershouldn’t be unregistered within your activity. Add it to the manifest with a intent filterCOUNTDOWN_STOPor whatever, to achieve this.