问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/8848121/why-it-is-impossible-to-access-resources-in-a-static-way