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?
This example demonstrates how do I pass data between activities in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
We can send the data using putExtra() method from one activity and get the data from the second activity using getStringExtra() methods. Example: In this Example, one EditText is used to input the text. This text is sent to the second activity when the “Send” button is clicked.
In this case, you should use startActivityForResult to launch your child Activity. This provides a pipeline for sending data back to the main Activity using setResult . The setResult method takes an int result value and an Intent that is passed back to the calling Activity.
MainActivity
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("extra_text", string);
startActivity(intent);
SecondActivity
String text = getIntent().getStringExtra("extra_text");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With