Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: passing parameters between classes

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);
like image 909
Yang Avatar asked Apr 06 '10 04:04

Yang


People also ask

How do you pass data between activities?

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 pass data from one activity to another activity in Android?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

What is a piece of data passed between activities when launching an intent?

Queries related to “is a piece of data passed between activities when launching an intent.” android studio send data to another activity.


1 Answers

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.

like image 93
Joshua Rodgers Avatar answered Oct 10 '22 20:10

Joshua Rodgers