0

http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html does not include a release method. Does it mean it will be GC-ed automatically? Thanks.

Joe C
  • 2,757
  • 2
  • 26
  • 46

1 Answers1

2

LocalBroadcastManager does not include a release method. Does it mean it will be GC-ed automatically?

No it will not. But it also does not have to.

It will exist from the point in time you fist call that method anywhere in your app until your app process is killed.

A static method named getInstance that returns you an instance of the class it is in, means in most cases that you're dealing with a singleton.

Singletons are objects that are intended to exist only once in your entire app and they behave like global variables (that is often considered bad for several reasons).

The whole broadcast mechanism would break down if the place in your code that register to receive broadcasts would use a different broadcast manager than the one the broadcasts are send over. The code makes sure that everybody uses the same one by making it a singleton.

LocalBroadcastManager will on the other hand not leak your activity context if you take that as parameter. It will call context.getApplicationContext() to get the application context which is itself a singleton that is safe to keep referenced forever.

The way the code creates the singleton and how to deal with context in singletons is described in further details in this article: Context, What Context? (Note: in a threadsafe way - unfortunately missing in that article)

Community
  • 1
  • 1
zapl
  • 63,179
  • 10
  • 123
  • 154
  • This is very helpful. Thanks. In general, how do we know if a returned obj is a singleton. Does it mean all ClassName.getInstance should return us a singletone? For example, how about (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); – Joe C Jan 25 '14 at 18:19
  • 2
    What `getSystemService` returns is one as well. A class that has no public constructor and you get objects of it's type via some other method are usually designed in a way that you a) don't have to know how many there are b) don't have to care about it's lifetime. `getInstance` is kind of a convention. `newInstance` would give you a new each time. Anything in between (e.g. a connection pool that has 5 connection objects) is probably `getInstance` as well (and you should not keep a reference to those forever) – zapl Jan 25 '14 at 19:08
  • So if I call getInstance on LocalBroadcastManager it will not be costly because it doesn't actually create a new one each time, but instead just reuse the old one -right? It is not a factory(?) – JohnyTex Mar 31 '14 at 09:28
  • 1
    @user2254314 Correct, it is pretty cheap and will not create new ones once it has created an instance. If you were calling the method over and over again (like 1000 times per second in a loop) you might notice that it's faster to keep your own reference to the instance since the [method is synchronized internally](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.2_r1/android/support/v4/content/LocalBroadcastManager.java#LocalBroadcastManager.getInstance%28android.content.Context%29). I'd still call it a factory since it does create an instance once. – zapl Mar 31 '14 at 13:07