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
  • 16,472
  • 26
  • 116
  • 210
Mars
  • 4,068
  • 11
  • 38
  • 60

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,528
  • 1
  • 18
  • 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,562
  • 1
  • 27
  • 50
Peter Knego
  • 79,504
  • 10
  • 122
  • 151
  • 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