I have a class2 which is involved by class1 when clicks are made. I have to pass some parameters/objects from class1 to class2. I only know the standard way which does not have an option of passing parameters.
// launch the full article
Intent i = new Intent(this, Class2.class);
startActivity(i);
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 the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.
Queries related to “is a piece of data passed between activities when launching an intent.” android studio send data to another activity.
You can use Intent.putExtra
(Which uses a Bundle
) to pass extra data.
Intent i = new Intent(this, Class2.class);
i.putExtra("foo", 5.0f);
i.putExtra("bar", "baz");
startActivity(i);
Then once you're inside your new Activity
:
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
float foo = extras.getFloat("foo");
String bar = extras.getString("bar");
}
This allows you to pass basic data to Activities. However, you may need a bit more work for passing arbitrary objects along.
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