I have a server running that notifies the user with a statusbar notification that opens my main activity, how can I pass data to my activity trough that intent?
Asked
Active
Viewed 2.8k times
2 Answers
31
MainActivity
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("extra_text", string);
startActivity(intent);
SecondActivity
String text = getIntent().getStringExtra("extra_text");

LifeInstructor
- 1,622
- 1
- 20
- 24
-
2Perfect answer. Short, and with the necessary. – Shudy Jul 13 '15 at 08:02
13
Use Intent.putExtra(..)
:
intent.putExtra("keyName", "somevalue");
This method is overloaded and takes various types as second argument: int, byte, String, various arrays..
To get the data out use appropriate getXYZExtra(). For String this is:
getStringExtra(String keyName)

CaptJak
- 3,592
- 1
- 29
- 50

Peter Knego
- 79,991
- 11
- 123
- 154
-
but how do I get the data in my activity? what's the event that is called when and activity gets an intent? – Mars Nov 06 '10 at 18:28
-
1
-
`getIntent()` is defined in Activity. You can call it anywhere inside activity, also inside `onCreate()` – Peter Knego Nov 06 '10 at 18:38
-
ok I tought I was calling getExtras from the wrong place but my mistake was that I wasent doing 'this.getIntent()' thanks for your help – Mars Nov 06 '10 at 18:44