Use an intent to send data to my activity

27,623

Solution 1

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)

Solution 2

MainActivity

Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("extra_text", string); 
startActivity(intent);

SecondActivity

 String text = getIntent().getStringExtra("extra_text");
Share:
27,623
Mars
Author by

Mars

Updated on February 29, 2020

Comments

  • Mars
    Mars about 4 years

    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?