How to listen to lifecycle events
Bundles have access to an instance of BundleContext that can be used to listen to different kinds of lifecycle events:
-
Bundle events are triggered by the runtime when the state of a bundle changes.
-
Framework events are triggered when the state of the runtime changes.
-
Service events are triggered when the state if a service changes.
Listening to bundle events
Using the addBundleListener
method.
Listening for bundle events
import BundleEvent from "apprt/BundleEvent";
export default class {
start(bundleContext) {
this._bundleEventListenerHandle = bundleContext.addBundleListener(this._onBundleEvent, this);
}
stop(bundleContext) {
bundleContext.removeBundleListener(this._bundleEventListenerHandle);
this._bundleEventListenerHandle = null;
}
_onBundleEvent(evt) {
if (BundleEvent.types.STARTED === evt.getType()) {
let bundle = evt.getBundle();
}
}
}
Listening to framework events
The same approach can be used to register to framework events, by applying the addFrameworkListener
method.
Listening for framework events
import FrameworkEvent from "apprt/launch/FrameworkEvent";
export default class {
start(bundleContext) {
this._frameworkEventListenerHandle = bundleContext.addFrameworkListener(this._onFrameworkEvent, this);
}
stop(bundleContext) {
bundleContext.removeBundleListener(this._frameworkEventListenerHandle);
this._frameworkEventListenerHandle = null;
}
_onFrameworkEvent(evt) {
if (FrameworkEvent.types.STARTED === evt.getType()) {
}
}
}