5

I was confused with the order that which function Activate function or bind function gets called when component become active. In my opinion activate function function will be called first as Bind function is for binding the service. But as we know that all the target service are first get into the component context then component gets activated.

Please clear my doubt.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77

1 Answers1

8

The activate method will be called after all the static references have been bound, i.e. after the bind methods have been called. So during the activate, you can be sure that the value of the static references will not change.

However for dynamic references, all bets are off. In fact the value of a dynamic reference could change multiple times in different threads, during the execution of the activate method.

UPDATE: You didn't ask about deactivation, but you might find this information useful all the same. The deactivate method will be called before any static reference is unbound. So for example: if you were bound to a service with a static reference and the service you were bound to goes away, then SCR will first call your deactivate, then your unbind method(s), and finally it will free the component instance for garbage collection.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
  • Thanks @Neil Barlett, Here i am referring to dynamic references.But i have one doubt that in bind method i am checking that componentcontext is null or not. When would be component context is null ? I think it would be null when component is not activated. – Sanjeev Shukla Apr 03 '13 at 09:40
  • You cannot get the ComponentContext if your component is not activated, because it is given to you in your activate method. So, there is no way for it to be null. However, you **probably** don't even need to use ComponentContext at all. I recommend not using it. – Neil Bartlett Apr 03 '13 at 20:26
  • One issue with that is if you use the `bind(ServiceReference ref)` signature: it requires the ComponentContext to resolve the reference via `componentContext.getBundleContext().getService(ref)`. But if bind() is called before activate() you have to remember the list of ServiceReferences bound so far temporarily and later resolve them in activate(). Slightly annoying :) – Alexander Klimetschek Feb 05 '16 at 03:31