Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent::putExtra() and EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

My code is as follows:

First, I was wondering about line 20:

I had two questions:

a. Why is MY_MESSAGE assigned to com.example.myfirstapp.MESSAGE? b. What is com.example.myfirstapp.MESSAGE? c. I mever made MESSAGE anywhere; is this automatically made like the variables in r.java file, or do i need to make it somewhere?

Secondly, about line 40: intent.putExtra(EXTRA_MESSAGE, message);

I am not sure if this method adds a message to the upcoming activity to be called or what... Partly, I am struggling to understand this due to not knowing the point of an Intent fully. I want to read my 200 fundamental section on what everything is, but I have set deadlines and I have been told not to take that approach for the time being for this project

With given the explanation of the Android Docs , I know an intent is:

The Intent itself, an Intent object, is a passive data structure holding an abstract description of an operation to be performed

A.) Could someone explain what the intent is used for or give some better quick articles than just the docs?

B.) Explain what putExtra( ) does and and these parameters more clearly:

  • name The name of the extra data, with package prefix.
  • value The String array data value
like image 922
Chris Okyen Avatar asked Dec 09 '22 22:12

Chris Okyen


2 Answers

An Intent is appropriately named; it's what you want to be done. As the documentation says:

Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.

By your code, you are familiar with starting an Activity via Intent:

new Intent(this, DisplayMessageActivity.class);

This uses your current Activity as the context from which to start the Intent, and gives the target class to launch. You already know this, I think. Basically, the Intent is just a guide for the Android device to follow so that it launches the right target with the right information.

Onto your real questions:

  1. "What is the intent used for?" This is described above; basically, it's used to tell the OS what your target is, where it's coming from, and what data it should provide. You've seen most of this in action without realizing; this constructor is the one you've been using, detailing the "from" and "to" portions. When you use putExtra, you are providing the Intent with data it can give to the "to" part of the code.
  2. The name parameter is best summed up by the documentation: "The name of the extra data, with package prefix." This is like a key in a HashMap; it is a string identifier of the content you are packaging. They tell you to use your package's prefix, just to prevent confusion. In your case, you should be using "com.SG.Three_Piece_Radio.YOURKEYNAME"; this does not have to be declared anywhere, nor is it a constant. Just a string. The value is just the contents of the extra (the data); this can be a ton of different things--short, int, String, Parcelable, and many more. (These can all be found in the various putExtras in the Intent docs.)

Once your Intent is received, you can use those same bits of data (for example, String myStr = getIntent().getStringExtra("com.SG.Three_Piece_Radio.YOURKEYNAME");) and do whatever you wish with them in the Activity you called.

like image 171
Cat Avatar answered Apr 01 '23 05:04

Cat


I think people have been very helpful here in giving great explanations about Intent itself and its purpose. I got to learn a lot from these answers.

However, there was a small aspect that I think needs a little more explanation.

So to answer your very first question that says :-

a. Why is MY_MESSAGE assigned to com.example.myfirstapp.MESSAGE? b. What is com.example.myfirstapp.MESSAGE? c. I mever made MESSAGE anywhere; is this automatically made like the variables in r.java file, or do i need to make it somewhere?

My answer would be :-

So as all explained, putExtra is meant for carrying additional information/data along with the intent for the new activity that is going to be started. This additional information that putExtra carries is given in Intent in the form of a Key-Value pair. In this Key-Value pair, the Key syntactically always has to be a String. In your case, the value is also a String and the "key" can be any random string. Now, to make sure that the system does not confuse your KEY with some other app's KEY you should always append the entire packet structure of the string along with it. And hence you use :-

com.example.myfirstapp.MESSAGE

where MESSAGE is actually the name of the key, (the String as is required, as I mentioned above) that would be associated with the string value that would be passed with the intent to the new activity.

Now you could have very well written the following as well :-

intent.putExtra("com.example.myfirstapp.MESSAGE", message);

instead of :-

intent.putExtra(EXTRA_MESSAGE, message);

but then this would reduce the flexibility of your code for changes to be made later. As for any changes in the key name you will have to change it everywhere. So to avoid this we rather assign the name of our key (in your case, MESSAGE) to a String variable (in your case EXTRA_MESSAGE).

This also makes it easier for other activities to reference this key by a simple String variable. And so to make it accessible to other activities (coupled with other self explained features)you make it as :-

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

Please correct me if I happened to miss something or went wrong somewhere.

like image 28
qre0ct Avatar answered Apr 01 '23 07:04

qre0ct