开发者

Android: passing parameters between classes

I have a class2 which is involved by class1 when clicks are made. I 开发者_如何学Gohave 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);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜