28

when passing extras such as Intent.putExtra("myName", myName), what's the convention for the name of the extra?

ie: if passing data between two activities, both would put/extract data under the id "myName", but should I just hardcode "myName" everywhere, or keep the value in the R.values.string?

schnatterer
  • 6,520
  • 6
  • 53
  • 71
damonkashu
  • 1,723
  • 5
  • 17
  • 24

2 Answers2

47

Hardcoding is definitely not an ideal solution.

The convention used in the Android framework is to create public static final constants named EXTRA_FOO (where FOO is the name of your key) like Intent.EXTRA_ALARM_COUNT

The actual value of the constant is a name spaced string to avoid conflicts: "android.intent.extra.ALARM_COUNT"

If you don't want to create dependencies between your Activities with constants, then you should consider putting these keys into string values within your strings.xml file. I tend to follow the same naming convention when defining the keys in xml:

<string name="EXTRA_MY_NAME">com.me.extra.MY_NAME</string>

It still reads like a static constant from the Java side:

getString(R.string.EXTRA_MY_NAME);
schnatterer
  • 6,520
  • 6
  • 53
  • 71
Eric Levine
  • 13,196
  • 5
  • 46
  • 48
  • 1
    This tutorial shows some sample code using the public static final String solution: http://developer.android.com/training/basics/firstapp/starting-activity.html#StartActivity – Moberg Jun 13 '13 at 10:25
  • I guess there is no easy way to share these constants between apps ? (one option is content provider, but it seems like an overkill) – Alexander Malakhov Sep 11 '13 at 09:07
  • 1
    @AlexanderMalakhov you could put them into an Android Library project, which your different apps could all use. – Eric Levine Sep 11 '13 at 11:53
  • Why hardcoding is not ideal? – mcont Jul 12 '14 at 17:21
  • @Matteo hardcoding the name is more error prone, since you may wind up having a typo in your code. If you use a static variable for the key when setting and accessing the data, then it ensures the name is consistent. – Eric Levine Jul 12 '14 at 17:32
  • 1
    @Matteo hardcoding is also difficult to maintain. If you want to change the name of the extra key, then you have to be certain to find every place that uses it and repeatedly make the update (without introducing any typos). If it is a variable, then you only need to change its value in one place. – Eric Levine Jul 14 '14 at 00:52
  • Ok thank you. Maybe in a complex application it's more useful to use a variable, but in an app with two simple activities and one argument (ex. "id") to be passed, maybe it's not that dangerous.. – mcont Jul 14 '14 at 07:37
  • What about "resulting" intents? Are there any best practices? The camera app for example just calls its extra `data`. See http://developer.android.com/training/camera/photobasics.html – schnatterer Jul 27 '14 at 13:54
  • @schnatterer You'd be better off asking that as its own question. You are referring to extras in the Intent, so its like naming keys in a HashMap. Don't hardcode the strings for the keys, but you don't have to namespace them like an Intent name. – Eric Levine Jul 29 '14 at 14:53
  • @elevine you're right! Let's see if there are more interesting thoughts on this. See http://stackoverflow.com/questions/25024108/best-practices-for-returning-data-to-an-activity-using-an-intents-extras – schnatterer Jul 29 '14 at 20:18
  • 11
    I disagree with using strings.xml, as I see it not very clean as a solution. I'm a newbie in android development, but strings.xml seems to be supposed to contain only strings that are displayable on the UI of the app. Strings that may need to be internationalized, and so on. Rather, I would store all of the keys in a separate class, be it ExtrasKeys, supposed to be shared among all of the activities. – alb-i986 Aug 05 '15 at 22:14
0

The only thing I saw in documentation is that extra keys should start from package name. However I do not fully follow this and the app works Ok so far.

I would prefer to use R.string.some_key within the code just to have it clean and dry.

Vit Khudenko
  • 27,639
  • 10
  • 56
  • 86