Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are the R.xxx.xxx resource id's static through versions?

If I were to store a string resource id in a database, could I expect the resource id to still point me to the desired string when I pull it back a few versions down the line?

Sample Case:

  • Store the recource Id 0x7f060003, with the assumption it points to the string "I'm a pleasant string!" for an inderterminate amount of time.

  • Time comes along in which I need the resource, but X versions have passed, will the resource id still point to "I'm a pleasant string!"?

like image 303
ahodder Avatar asked Nov 28 '22 22:11

ahodder


2 Answers

Use getResources().getIdentifier() instead. This way you can retrieve your ressource by its name, even if its id changes. Here's an example:

int resID = getResources().getIdentifier("nameofthedrawable", "drawable", "com.your.project");

You can then save just the name of the ressource in the database, and retrieve the ressources later from the database.

See this link for more info.

like image 187
Noureddine AMRI Avatar answered Dec 09 '22 19:12

Noureddine AMRI


When you say X versions have passed, do you mean you have changed your source code? And updated the apk?

The R id integers are created at compile time. Therefore constant in your app once built.

However if you add a new layout / id / drawable your ID's will change.

like image 22
Blundell Avatar answered Dec 09 '22 20:12

Blundell