0

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?

CodeZero
  • 179
  • 2
  • 15

2 Answers2

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.

Community
  • 1
  • 1
nbaroz
  • 1,795
  • 13
  • 14