Android:在类之间传递参数

时间:2010-04-06 04:48:55

标签: android parameters android-intent

点击发生后, class1 会涉及 class2 。我必须将一些参数/对象从 class1 传递给 class2 。我只知道没有传递参数选项的标准方法。

// launch the full article
Intent i = new Intent(this, Class2.class);

startActivity(i);

1 个答案:

答案 0 :(得分:14)

您可以使用Intent.putExtra(使用Bundle)传递额外数据。

Intent i = new Intent(this, Class2.class);
i.putExtra("foo", 5.0f);
i.putExtra("bar", "baz");
startActivity(i);

然后,当您进入新的Activity

Bundle extras = getIntent().getExtras(); 
if(extras !=null)
{
 float foo = extras.getFloat("foo");
 String bar = extras.getString("bar");
}

这允许您将基本数据传递给活动。但是,您可能需要更多的工作来传递任意对象。