Here is the discussion from the Sling docs:
It may well be that the component needs to be notified, when it is activated and deactivated. For this, the component may implement an activate method and a deactivate method. Both methods must be public or protected and take a single argument, the org.osgi.service.ComponentContext. It is recommended for this method to the protected as it is only used by the Service Component Runtime and should of course not be part of the public API of the component.
Read the following Sling topic for more information about this:
http://felix.apache.org/documentation/subprojects/apache-felix-service-component-runtime.html
When you create an Adobe Archetype project using Maven - you get this class that contains these methods:
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Just a simple DS Component
*/
@Component(metatype=true)
@Service
public class SimpleDSComponent implements Runnable {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private BundleContext bundleContext;
public void run() {
logger.info("Running...");
}
protected void activate(ComponentContext ctx) {
this.bundleContext = ctx.getBundleContext();
}
protected void deactivate(ComponentContext ctx) {
this.bundleContext = null;
}
}
See: https://helpx.adobe.com/experience-manager/using/first-osgi.html