114

I would like to pass some variables in the constructor of my ListActivity

I start activity via this code:

startActivity(new Intent (this, viewContacts.class));

I would like to use similar code, but to pass two strings to the constructor. How is possible?

Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • 2
    I think it would be a better question if it asked how to pass the parameters to a new Activity as well how to get the parameters once you pass them. I dont understand why we need to have 2 separate questions if you need to use both to really have any functionality anyways. (Thats why i included how to get parameters in my answer look below) – Xitcod13 Oct 18 '12 at 00:58

3 Answers3

261

In order to pass the parameters you create new intent and put a parameter map:

Intent myIntent = new Intent(this, NewActivityClassName.class);
myIntent.putExtra("firstKeyName","FirstKeyValue");
myIntent.putExtra("secondKeyName","SecondKeyValue");
startActivity(myIntent);

In order to get the parameters values inside the started activity, you must call the get[type]Extra() on the same intent:

// getIntent() is a method from the started activity
Intent myIntent = getIntent(); // gets the previously created intent
String firstKeyName = myIntent.getStringExtra("firstKeyName"); // will return "FirstKeyValue"
String secondKeyName= myIntent.getStringExtra("secondKeyName"); // will return "SecondKeyValue"

If your parameters are ints you would use getIntExtra() instead etc. Now you can use your parameters like you normally would.

ereOn
  • 53,676
  • 39
  • 161
  • 238
Xitcod13
  • 5,949
  • 9
  • 40
  • 81
  • How can we ensure `getIntent()` returns the intent we created in your first code snipper? – Zhou Haibo Jun 01 '23 at 01:16
  • when you start an Activity you pass in an intent. This is the intent that will be returned. notice: `myIntent.putExtra("secondKeyName","SecondKeyValue"); startActivity(myIntent);` – Xitcod13 Jun 03 '23 at 23:05
43

I think you want something like this:

Intent foo = new Intent(this, viewContacts.class);
foo.putExtra("myFirstKey", "myFirstValue");
foo.putExtra("mySecondKey", "mySecondValue");
startActivity(foo);

or you can combine them into a bundle first. Corresponding getExtra() routines exist for the other side. See the intent topic in the dev guide for more information.

RickNotFred
  • 3,381
  • 2
  • 24
  • 26
  • 3
    Here's an example of how to get these values in the launched activity: http://stackoverflow.com/a/4233898/246743 – Ted Avery Aug 29 '12 at 15:18
2

putExtra() : This method sends the data to another activity and in parameter, we have to pass key-value pair.

Syntax: intent.putExtra("key", value);

Eg: intent.putExtra("full_name", "Vishnu Sivan");

Intent intent=getIntent() : It gets the Intent from the previous activity.

fullname = intent.getStringExtra(“full_name”) : This line gets the string form previous activity and in parameter, we have to pass the key which we have mentioned in previous activity.

Sample Code:

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("firstName", "Vishnu");
intent.putExtra("lastName", "Sivan");
startActivity(intent);
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81