Why and when to unregister Android content

I am developing an Android application and working with content watchers for the first time. So I read a lot of tutorials and realized why it is provided and how to use it.

Now I read these blog posts which

Do not forget to unregister observers

So, I can’t understand if I want to register ContentObserver, what could be the situation in which I should cancel it.

If the goal of the observers is fulfilled, then this is normal, we can cancel the registration. But I have to watch until the user uninstalls the application, then why am I going to unregister the observer.

Here is the url of this blog

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

+5
source share
4 answers

Imagine you have an application that allows you to use the friend list screen created with FriendListActivity. Now, to create a user interface for this action, you read the data from the database and create your user interface.

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

, , . , - FriendListActivity Activity URI (, URI), , , FriendListActivity. , , ( ), .

, , ...

, - ( URI). , , Activity , .

, ...

+4

:

Activity Life Cycle

  • onCreate, onDestroy
  • onStart, onStop
  • onResume, onPause

, .

+9

, ,

onResume(), onPause().

, .it observe memory leak.

  @override

    public void onDestroy()
    {

getContentResolver().
      unregisterContentObserver(yourObserver);

    }

 @override

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

    }
+6

:

, . , .

, Activity , - onPause. onPause , Activity.

Activities: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

+2

All Articles