Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why it is impossible to access resources in a static way?

I know now, that if I need to get a recource in some static function, I have to pass context or recources of the context there somehow - by parameter or through a static variable. But why is it neccessary? The id's of the resources are reachable in static surroundings, for example R.string.some_my_stuff. If I want a system resource, it is also visible there through Resources.getSystem().getString(android.string.some_common_stuff). But why can't I do something similar to get an application resource? The resource files are the usual static part of the sources. Resources are static and belong to application. The classes of application belong to it in the same way and I can access their static parts in a static way.

Why can't I use resources in all the application in same static way, which would be the most natural, but have to access them through instance instead?

I am afraid, I do not understand something very important.

Please, don't repeat that I can't do it. I know it, on my honour. Please, explain why, or show me the way... Only that will cure me from my sadness :-) Thank you.

like image 245
Gangnus Avatar asked Jan 13 '12 09:01

Gangnus


1 Answers

The resource IDs are unique per application, they are not unique over all application (including the Android system). E.g. there may be two different string in different applications which have the same ID, say 42. Therefore yon may access only one application statically (every programmer must agree which one that is, its the Android system (there no choice, its the only one always installed)). For all the other application you must be able to tell the system which application's resources you want to access. You do this using the context.

like image 134
Stefan Avatar answered Sep 19 '22 11:09

Stefan