15

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?

trejder
  • 17,148
  • 27
  • 124
  • 216
Mars
  • 4,197
  • 11
  • 39
  • 63

2 Answers2

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
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
    `activity.getIntent()` or `this.getIntent()` – Peter Knego Nov 06 '10 at 18:32
  • `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