Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the use of @StringRes in javadoc?

I wonder for a method like MyDialog(Context context, @StringRes int message), what does @StringRes exactly do? My impression is that if it is not there, the programmer might think message is a number or something? Is that only a hint for the developer or does it do a more important job?

like image 980
djnotes Avatar asked Dec 19 '22 01:12

djnotes


2 Answers

@StringRes indicates that the integer to be passed is a String Resource (from values/strings.xml). For Example:

R.string.title

Reference: https://developer.android.com/reference/android/support/annotation/StringRes.html

like image 145
Tenten Ponce Avatar answered Jan 02 '23 12:01

Tenten Ponce


You got it right, these annotations will help developer who will use it, also it will help in code inspection tools like Lint, which is supported by IDE Android Studio itself and while building command line

Below is except from Android documentation

Using code inspections tools such as Lint can help you find problems and improve your code, but inspection tools can only infer so much. Android resource IDs, for example, use an int to identify strings, graphics, colors, and other resource types, so inspection tools cannot tell when you have specified a string resource where you should have specified a color. This situation means that your app may render incorrectly or fail to run at all, even if you use code inspection.

Annotations allow you to provide hints to code inspections tools like Lint, to help detect these more subtle code problems. They are added as metadata tags that you attach to variables, parameters, and return values to inspect method return values, passed parameters, local variables, and fields. When used with code inspections tools, annotations can help you detect problems, such as null pointer exceptions and resource type conflicts.

Android supports a variety of annotations through the Annotations Support Library. You can access the library through the android.support.annotation package.

like image 43
Akhil Avatar answered Jan 02 '23 13:01

Akhil