Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

android - What is the concept behind R.java?

In android R.java is used to provide access to resources defined in XML files. To access the resource we need to invoke findViewById() method passing in the id of the resource to be fetched.

This is similar to Spring where beans are defined in a XML context and are fetched by using application context. context.getBean("beanId")

This provides loose coupling since the beans are defined externally and could be changed without making modifications to the code.

This has me confused. Though what Android does looks similar to spring, what advantage does it offer?

  1. What is the point of having an intermediate R.java anyway? Couldn't we just acquire resources directly from XML by use of a resource reader/application context. e.g. findViewById("resourceId")
  2. There isn't any loose coupling. Since references in R.java get auto-generated how could one delete a resource and put in a new one?
  3. What design pattern does it follow(if there is one)?
  4. Wouldn't it be better to have resources injected using IOC (like Roboguice)? Why did then google decide to give us such a wierd way of working with resources?

Pardon my ignorance. I'm a newbie Java developer trying too many things at once. :-) Thanks for all the feedback.

question from:https://stackoverflow.com/questions/10004906/what-is-the-concept-behind-r-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

android.R.java is not just where XML ids are stored. It also contains access to resources - such as drawables, layouts, strings, arrays, and basically anything you can declare in resources.

Personally I find that it is useful when using Eclipse. I can simply type findViewById(R.id. and Eclipse will show a tooltip with a list of options to choose from.

However at a platform level, I would say that the hardcoded id variables help prevent errors when using Strings to identify resources -- something that can be debuggable while programming (or during compilation, rather than runtime).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...