From several classes in my app I want to get String global values without using Context.
If I declare this variables in Application class, SharedPreferences or Strings.xml, then I must use Context for get/set it values.
Using Singleton is not a good practice as I understand from this post.
Is there any good way for using global variables without using context?
Create a global class or store the string in a place that “makes sense”. For instance if you have the class Shoes you could do this:
Now you can do this:
You will find there are many things like this in Android. It would be even better if you used enums instead. Hope this helps.
UPDATE
If you would like to use setters and getters then there are 2 solutions:
You can take advantage of the static initializer and static methods. You could just remove the final declaration and do what you want with the Strings, like this:
Or use good encapsulation practices and do this:
However I must note: If you are doing this because you cannot get a Context then you are missing something. A context can be obtained from anywhere – objects instantiated by the system are given a context as a parameter. If you have your own custom object you can just pass the ApplicationContext as a parameter or the class using the object itself (this).