问题
I want to implement a feature toggle in OSGI, which means I want to start and stop bundles programmatically.
For plug-ins that's really easy:
Bundle plugin = //...
plugin.start();
plugin.stop();
For fragments it's not so easy. Stopping does not work at all, and if I use uninstall() the fragment is gone for good, and I'm not able to install it again, since I don't know the correct location for BundleContext#installBundle(...) (and looking at Bundle#getLocation(), neither does the fragment itself).
The problem I'm facing is I have a fragment contributing to some Eclipse extension points. Just stopping the host plug-in will not remove these services (for whatever reason). So I need to stop the fragment manually (or maybe clear that stupid Eclipse cache that keeps screwing applications up?).
So how can I install fragments at runtime?
回答1:
Fragments do not have their own life cycle. They are part of the life cycle of their host bundle. That means there is no way to start/stop them independently of the host.
If you need more background about bundles, fragments and life cycles, I advise you to read the OSGi specification, which can be downloaded from the http://osgi.org/ website.
来源:https://stackoverflow.com/questions/29773056/start-stop-fragment-programmatically