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?
@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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With