Ken
Ken

Reputation: 297

why android need to use meta-data?

i found some AndroidManifest.xml have some .

like this:

<meta-data android:name="zoo" android:value="@string/kangaroo" />

code in Android:

ApplicationInfo ai = getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
String zoo = ai.metaData.getString("zoo");

i can define a string value in string.xml ,and get it in code very easy.

what's the different between and string.xml?

Upvotes: 4

Views: 3702

Answers (1)

Simon
Simon

Reputation: 14472

This is a bad example. It looks like the value is data used by the app. The manifest is the correct place to store meta-data about the app. Placing string data here which is used by the app (i.e. consumed) is also not localisable.

In one of my apps, I have static content in a library. That library has it's own version number and is shown to the user in the "about" activity. I could store this as a string constant within the code but it is more logical, and maintainable, to use manifest meta-data where all of the app version information is stored alongside the SDK versions etc.

To summarise, in the example you have given, the @string/kangaroo value should be in strings.xml.

Upvotes: 3

Related Questions