What is the concept behind R.java?

后端 未结 4 927
孤独总比滥情好
孤独总比滥情好 2020-12-01 13:29

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

4条回答
  •  臣服心动
    2020-12-01 14:09

    The comparison feels somewhat a little bit (actually) weird, because you compare two mechanism based on the fact that they use named things for doing stuff. For resource loading, e.g., have a look at how resource handling is done in the .Net world.


    It provides for compile-time checking if the resource is available. Because if it not, there will not be a static inside R.java that points to it. In the Spring example, how can you be sure there is a bean called beanId? It does not provide for checking if it is the right type of resource, though.

    Why is this not loose? As long as the new resource has the same name, it will generate the same static constant. In Spring, you would have to use the same bean name.

    Design Pattern? None. It just adds one level of indirection by naming the resources and then refer to them only by name, not by directly loading them from their true location.

    Actually, the resources are injected, because resource loading must cope with localization. See here for how Android does stuff; in the .Net world, additional cultures are packed into satelite assemblies; the resource manager will load the right one based on the current culture.

提交回复
热议问题