Home » Android » java – Why and when to unregister content observers in android

java – Why and when to unregister content observers in android

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am developing an android application and first time working with content observers. So I read a lot of tutorials and got the concept that why it is provided and how to use.

Now I have read these lines on a blog that

Dont forget to unregister content observers

So, I am not able to understand if I want to register a ContentObserver then what may be the situation in which I have to unregister it.

If the purpose of the observers get fulfilled then its ok, we can unregister it. But I have to observe till user uninstalls the application, then why I will be going to unregister the observer.

Here is the url of that blog

http://www.grokkingandroid.com/use-contentobserver-to-listen-to-changes/

How to&Answers:

Imagine you have an application that has let’s say a friend list screen created using a FriendListActivity. Now to create UI for this activity you read data from the DB and create your UI.

Now imagine that you also have a service that runs in background and syncs this database with a remote database using web services.

Now whenever the service updates the friend list in the DB you want to also update your UI accordingly. So, to achieve this you would register content observer in your FriendListActivity at the time of Activity launch for Friends URI (assuming that know the concept of URI), so that whenever the friend list in database changes you would also update friends list in your FriendListActivity. Now if the user moved out of your activity or closes the app then you don’t need to update your UI (so you don’t need to get event for DB changes), so you would unregister the content observer.

Now the next time user comes to this screen, you would again fetch the latest data and register with the DB for content changes…

So the content observer is normally a relation between an activity and database (or specific URI). SO you would register it when the Activity gets launched and un-register it when the activity goes out of the view..

Hope this helps…

Answer:

Have a look at the activity lifecycle:

Activity lifecycle

  • If you register an observer in onCreate you unregister in onDestroy
  • If you register an observer in onStart you unregister in onStop
  • If you register an observer in onResume you unregister in onPause

Since only one activity can be shown at the same time, I would recommend using the last option.

Answer:

please read a link you provided also ,in that clearly mention

You register your observer in the onResume() lifecycle method and you unregister it in the onPause() method.

once you unregister it one of these method’s .it will not observe any value and ll not give any memory leak also.

  @override

    public void onDestroy()
    {

getContentResolver().
      unregisterContentObserver(yourObserver);

    }

or you can do this also

 @override

    public void onPause()
    {
    getContentResolver().
  unregisterContentObserver(yourObserver);

    }

Answer:

In the blog post, it states:

When you have registered a content observer, it is your responsibility to also unregister it. Otherwise you would create a memory leak and your Activity would never be garbage collected.

So when your Activity is closed you should unregister – this should be done in the onPause method. onPause will always be called, whenever you go away from your Activity.

Check out the lifecycle for Activities: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle