Is resource resource R.xxx.xxx static across versions?

If I were to store a string resource identifier in a database, can I expect the resource identifier to point to the desired string anyway when I pull it down a few lines?

Example:

  • Keep the source code identifier 0x7f060003 , with the assumption that it points to the line "I'm a nice line!" for inderterminate amount of time.

  • The time comes when I need a resource, but the X versions have passed, whether the resource identifier will point to "I'm a nice line!" ?

+4
source share
3 answers

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

Integers R id are created at compile time. Therefore, the constant in your application was once built.

However, if you add a new layout / id / drawable, your identifier will change.

+4
source

Use getResources () instead of getIdentifier (). Thus, you can get your resource by its name, even if its identifier changes. Here is an example:

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

Then you can save only the ressource name in the database and retrieve data from the database later.

See the link for more details.

+18
source

You can fix resource identifiers by creating public.xml in the res / values ​​directory. Here is the one I used:

 <?xml version="1.0" encoding="utf-8"?> <resources> <public type="drawable" name="button" id="0x7f020000" /> <public type="drawable" name="button_flat" id="0x7f020001" /> <public type="drawable" name="button_flat_square" id="0x7f020002" /> <public type="drawable" name="button_flat_vert" id="0x7f020003" /> <public type="drawable" name="button_grey" id="0x7f020004" /> <public type="drawable" name="right" id="0x7f020005" /> <public type="drawable" name="left" id="0x7f020006" /> <public type="drawable" name="up" id="0x7f020007" /> <public type="drawable" name="down" id="0x7f020008" /> <public type="drawable" name="red" id="0x7f020009" /> <public type="drawable" name="green" id="0x7f02000A" /> <public type="drawable" name="yellow" id="0x7f02000B" /> <public type="drawable" name="blue" id="0x7f02000C" /> <public type="drawable" name="plus" id="0x7f020010" /> <public type="drawable" name="minus" id="0x7f020011" /> </resources> 
+1
source

Source: https://habr.com/ru/post/1386776/


All Articles