Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use an intent to send data to my activity

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?

like image 324
Mars Avatar asked Nov 06 '10 18:11

Mars


People also ask

How do you pass data between activities using 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.

How do you send data to activities?

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.

What is used to send back data to the activity?

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.


1 Answers

MainActivity

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

SecondActivity

 String text = getIntent().getStringExtra("extra_text");
like image 87
LifeInstructor Avatar answered Sep 22 '22 22:09

LifeInstructor