我有一个class2,当点击时它被class1 所涉及。我必须将一些参数/对象从 class1 传递到 class2。我只知道没有参数传递选项的标准方式。
// launch the full article
Intent i = new Intent(this, Class2.class);
startActivity(i);
最佳答案
您可以使用 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");
}
这允许您将基本数据传递给 Activity 。但是,您可能需要做更多的工作才能传递任意对象。
关于Android:在类之间传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2582690/