Why initialize the key of extra?

2020-03-26 05:05发布

问题:

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.

回答1:

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.



回答2:

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



回答3:

  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



回答4:

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