I need to make one instance of some class – and this one instance need to be accessible from any place in the code.
So, I found the Guice… and i want to use the ‘@Singleton’ from this package but i don’t find any example or some doc to how to use it and how to make the declaration.
Ok, My answer is not a specific for @Singleton of Guice, But If you want to Make a class which is accessible through all your activities then I think,You have to use Application class of Android. (This is my personal opinion for your need)
The way to do this is to create your own subclass of
android.app.Application, and then specify that class in the application tag in your manifest. Now Android will automatically create an instance of that class and make it available for your entire application. You can access it from any context using theContext.getApplicationContext()method (Activity also provides a methodgetApplication()which has the exact same effect):This has essentially the same effect as using a static variable or singleton, but integrates quite well into the existing Android framework. Note that this will not work across processes (should your app be one of the rare ones that has multiple processes).
Here is the nice tutorial about how to use it, Extending the Android Application class and dealing with Singleton