views:

353

answers:

1

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);
+3  A: 

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.

Joshua Rodgers
which means only simple types (int, String, double) can be passed but not Classes? Even BigMap cannot be passed?
Yang
You can pass serializable and "parcelable" objects along, too.http://developer.android.com/reference/android/content/Intent.htmlI'll look around to see what I can find on making passing objects easier. One consideration is to make a static field on the activity class that you can set to the reference of the object before starting the activity.
Joshua Rodgers