3

When we want an intent to carry some data to another application component, we use an extra of that intent. An intent is simply a key value pair. We first define our key as a public constant, and give it a value. e.g.

public static final String extra_key = "com.example.myapp.MESSAGE";

We also have to assign the key the data which needs to be carried by the intent. e.g.

String extra_value = editText.getText().toString();

Then we make an extra of the intent like:

intent.putExtra(extra_key, extra_value);

MY QUESTIONS:

  1. Why does the key have to be public?

  2. Why do we need to intialize the key in the first place, why can't we just declare it, because it will be assigned a value (the data to be carried by the intent) anyway. So why couldn't we do something like public static final String extra_key;

  3. I have read that the key value should include the reverse domain name so that it is unique in case other packages plunge in. But what is the point of giving it a unique value when it will anyway be assigned another value which is the data to be carried by the intent.

Thank you in advance.

L.Butz
  • 2,466
  • 25
  • 44
user2882662
  • 535
  • 1
  • 6
  • 18

4 Answers4

4

Why does the key have to be public?

It doesn't. This is not a question about intent extras or key value pairs. It is simply a question about Java variable scope and visibility.

In calling class:

intent.putExtra("KEY_NAME", "Key_Value");

In receiving component:

intent.getStringExtra("KEY_NAME");

This work just fine. Good practice is to make it public final static so that the sender and receiver can use the same constant name.

Why do we need to intialize the key in the first place, why can't we just declare it, because it will be assigned a value (the data to be carried by the intent) anyway. So why couldn't we do something like

See above. The key name is nothing more than a string. The key does not carry the data, the value does.

I have read that the key value should include the reverse domain name.

This makes no sense. The key value is whatever data the sender wants to send to the receiver. Or did you mean the key name? The name of the key must be know by the receiver so if this intent is to start an external component, then you must use the key name as defined by the receiver. If the intent is for an internal component, then you define the name to be whatever you want. I can see no good reason to include the package name. It just uses more memory.

Simon
  • 14,407
  • 8
  • 46
  • 61
  • I've never included the package name to send data from one activity to another in my apps either. It makes no sense to do so. – NickT Nov 03 '13 at 12:03
  • OK, the key doesn't carry the data but the value does, I understand. But my question is why ever do we need to assign any value to the key, why can't we just declare it as a string but not give it a value? because I don't see any use of "com.example.myapp.MESSAGE" anywhere. – user2882662 Nov 03 '13 at 12:05
  • @NickT but the docs say `public Intent putExtra (String name, String value)` Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll". although i have not used it myself – Raghunandan Nov 03 '13 at 12:11
  • @user2882662 - because each putExtra will have a corresponding getExtra somewhere, then it makes sense to define this in one place only. – NickT Nov 03 '13 at 12:11
  • @NickT: Please go to this page: http://developer.android.com/training/basics/firstapp/starting-activity.html and find this line: It's generally a good practice to define keys for intent extras using your app's package name as a prefix. This ensures they are unique, in case your app interacts with other apps. – user2882662 Nov 03 '13 at 12:11
  • If you are just doing startActivity(), then other apps can't get to your activity, so it's OTT. – NickT Nov 03 '13 at 12:13
  • @NickT It is a key value pair. getExtra(extra_key) will use the name of the string, i don't see any use of assigning a value to the key when we declare it. Where is this value used? – user2882662 Nov 03 '13 at 12:14
  • Over The Top (i.e. not really necessary), as for assigning a value that's just defining the key as public String key = "keyname", so both the put and the get use the same thing – NickT Nov 03 '13 at 12:17
  • 1
    The key name is a string, just like any other. How and where you declare it, if you use a variable at all, is entirely up to you. Only good practice determines that you use a shared variable (hence `public static final`), there is NO requirement to do so. Consider this. Even if you prefix the name with your package name and pass it to an external component, the external component can only use it if it knows your package name and suffix. It could determine the package name from the intent action but how would you know that? What is the contract for usage? – Simon Nov 03 '13 at 12:19
  • @NickT: public is enough for making it s sorta shared variable, static final to make it a constant, but why give it a value at all? why assign it "com.example.myapp.MESSAGE" or any value. – user2882662 Nov 03 '13 at 12:28
  • @user2882662 because public static final String aStringVariable; won't compile. You have to do public static final String aStringVariable = "something"; – NickT Nov 03 '13 at 12:34
  • @NickT: That's my question. Why doesn't it compile. What does the compiler use the value for? – user2882662 Nov 03 '13 at 12:42
  • @Simon: I don't know what you mean by intent action, I am new to learning android development. – user2882662 Nov 03 '13 at 12:57
  • @user2882662 - This is my last word on the matter - if you declare a variable as 'final', you have to say what that final value is. – NickT Nov 03 '13 at 13:00
  • I am sorry if I have infuriated you @NickT Thank you very much for your answers. If somebody else can kindly answer this, I'll be grateful. Why do we need to assign a value to the key when it is being declared?What is the use of that value? It has been said that when a final is declared, a value must be assigned so it is known what the constant is. Sounds like common sense. But if I simply declare a variable public static final String a; the compiler does not complain at all, which means initializing a final variable with a value is not a must, as long as it is initialized before it is used. – user2882662 Nov 03 '13 at 14:01
  • When I declare `public static final String a`, I do get a warning that a may not have been initialized. You can indeed do that with Android too, *assuming that you still initialize the final field* in a `static { ... }` block. – echristopherson Feb 11 '14 at 05:19
1

The key is used to store and retrieve the value. You can thought it like a key for an HashMap.

  1. Because you could want to reuse the same key in multiple place (for instance store a value in an activity and retrieve the value in anaother one)
  2. when you member as static that means that is not belongs to a particular instance of an object, but all the objects of that class are going to share it. If you remove the static keyword you can init your variable inside the constructor but, in this case, it belongs to the object instance, and it is not a constant

If you need clarification, feel free to ask

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0
  1. Why does the key have to be public?

You probably want to use the key in a different class to get the values so you have it static and public.

In the other activity

   String s = getIntent().getStringExtra(MainActivity.extra_value);
   // considering extra_value is static in MainActivity 

You need not have it that way.

 intent.putExtra("mykey", extra_value);

Then to retrieve in the other activity

  String s = getIntent().getStringExtra("mykey");  // keys must match

Variable extra_key is declared as string and initialized and it is static.

The static modifier, in combination with the final modifier, is also used to define constants. The final modifier indicates that the value of this field cannot change.

Constants defined in this way cannot be reassigned, and it is a compile-time error if your program tries to do so.

Check the topic constants.

public static final String extra_key= "com.example.myapp.MESSAGE";

Java: define terms initialization, declaration and assignment.

http://developer.android.com/reference/android/content/Intent.html

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • But what is the use of the value "com.example.myapp.MESSAGE". I don't think it is used anywhere ?? – user2882662 Nov 03 '13 at 12:18
  • @user2882662 you need to initialize it and you need the key when retrieving the string in the other activity – Raghunandan Nov 03 '13 at 12:19
  • @user2882662 If a primitive type or a string is defined as a constant and the value is known at compile time, the compiler replaces the constant name everywhere in the code with its value. pls check the topic constants http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – Raghunandan Nov 03 '13 at 12:21
  • Yes I have read about compile time constants. But I still don't see any good in replacing extra_key by its value everywhere. What's the use? What's the use of giving it a value? The key can be retrieved from the other activity using the name of the key. – user2882662 Nov 03 '13 at 13:05
  • @user2882662 yes you can do this `intent.putExtra("mykey, extra_value);` and to retrieve `String s = getIntent().getStringExtra("mykey")` and you need to use this `public static final String extra_key= "com.example.myapp.MESSAGE";` – Raghunandan Nov 03 '13 at 13:25
  • Why do we need `= "com.example.myapp.MESSAGE"` Why doesn't just `public static final String extra_key;` work? – user2882662 Nov 03 '13 at 14:19
  • Because Java then doesn't know the *value* of the constant. (Some languages, like Ruby and Lisp, do have a "symbol" data type which can effectively be used as both a constant's identifier and its value; but Java doesn't have that.) – echristopherson Feb 11 '14 at 05:25
0

I just want to add that that package name as in key identifier, as I see in comments, is not a must but it's rather a good practice.

According to this https://developer.android.com/training/basics/firstapp/starting-activity.html#BuildIntent

Vu N.
  • 35
  • 9