i want to know the usage of putExtra from very basic level
2 Answers
If you want add information to your intent you can use this method. This information is represented as tuple (key, value). There are the number of value types that can be included into the extras of intent (for instance, int, int[], Bundle, Parcelable, and so on). For each this method there is a corresponding "read" method that is used to get the information from the intent.
So here is a possible example how to use this. Imagine that you want explicitly call the activity B from activity A and pass to it an array of integers:
int intArray[] = {1,2,3,4};
Intent in = new Intent(this, B.class);
in.putExtra("my_array", intArray);
startActivity(in);
To read the information in activity B (in onCreate() method) you should use the following code:
Bundle extras = getIntent().getExtras();
int[] arrayInB = extras.getIntArray("my_array");
-
-
The return type of the function can be easily found in the documentation: developer.android.com/reference/android/content/…, double[])– YuryApr 8, 2014 at 8:35
-
It says the return type is
Intent
. Please correct me if I've misread the documentation. Also, if it is adouble[]
, should it not be a 2D array as it needs to store tuple values? Apr 8, 2014 at 8:42 -
No, you're right. The return type is Intent. As for the second question, you can see that there are several overloaded
putExtra
methods.– YuryApr 8, 2014 at 10:35
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".
Parameters:
name: The name of the extra data, with package prefix.
value: The double array data value.
Returns the same Intent object, for chaining multiple calls into a single statement.