Best place/way to register and de register for contentObserver

250 views Asked by At

I have an use case where I have to register for content observer in my android app. I want to listen right from app start till my app is closed, but my application contains many activities, which is the best place to register and de-register for the observer?

If I do that in my base activity's onResume and onPause, registering and deregistering would happen frequently (as the activities also change during user actions), is there a better place to register and de register?

Thanks, Rishi.

1

There are 1 answers

3
Gabe Sechan On

If you want to do things across multiple activities, use a Service. Have each activity start the Service (if its already running that won't cause a second to start) and have it register. If you need to access data from that observer, use the Binder to define an API and bind to the service.