I implemented a broadcast receiver to update one of my UI activities but my problem is this: when the back button is pressed by the user, android kills the broadcast receiver but when the home button is clicked, the state is retained. What am trying to achieve is retaining the state even after the back buttton is clicked-- onBackPressed()
until the user explicitly calls finish()
. Any pointers?
Asked
Active
Viewed 134 times
0

CodeZero
- 179
- 2
- 15
-
Do you call unregisterReceiver() from onBackPressed() ? – nbaroz Feb 15 '15 at 08:39
2 Answers
1
You shouldn't rely on your Activity
being always accessible to receive the broadcast. Instead, use LocalBroadcastManager to attempt to deliver the broadcast to the Activity
, and if it fails - store the update locally and apply it when the Activity
is displayed next time.

Egor
- 39,695
- 10
- 113
- 130
0
You need to register your receiver in your onCreate()
method and call unregisterReceiver()
on onDestroy()
.
Edit:
You can see @Shiki's answer in here. It does what you need.