7
我有一个class2这是由类1当涉及点击时。我必须将class1中的一些参数/对象传递给class2。我只知道没有传递参数选项的标准方式。Android:在类之间传递参数
// launch the full article
Intent i = new Intent(this, Class2.class);
startActivity(i);
我有一个class2这是由类1当涉及点击时。我必须将class1中的一些参数/对象传递给class2。我只知道没有传递参数选项的标准方式。Android:在类之间传递参数
// 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");
}
这使您可以通过基础数据的活动。但是,您可能需要更多工作来传递任意对象。
这意味着只有简单的类型(int,String,double)可以被传递但不是类?即使BigMap不能被传递? – Yang 2010-04-06 05:06:52
您也可以传递可串行化和“可分类”的对象。 http://developer.android.com/reference/android/content/Intent.html 我会环顾四周,看看我能找到什么让传递对象更容易。一个考虑是在活动类上创建一个静态字段,您可以在开始活动之前将其设置为对象的引用。 – 2010-04-06 05:13:48
使用旅行车。它使得它非常简洁:https://github.com/beplaya/Wagon – beplaya 2014-03-18 22:23:24