which one is called first Activate function or bind function in SCR Runtime
Asked Answered
B

1

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.

Blanton answered 3/4, 2013 at 8:49 Comment(1)
What the question is?Westfall
R
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.

Reflex answered 3/4, 2013 at 8:53 Comment(3)
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.Blanton
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.Reflex
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 :)Sinew

© 2022 - 2024 — McMap. All rights reserved.