以下根据参数类型分别介绍Intent传递方法。
一、参数类型为常用类型,如String、Integer等
发送如下
intent.PutExtra("msg", "start");
intent.PutExtra("current", current);
接收如下
string s = intent.GetStringExtra("msg")
int cur = Intent.GetIntExtra("current", 0);
二、参数类型为List类型或自定义对象类型
1. 单纯的传递List<String> 或者List<Integer>的话 就可以直接使用
intent.PutStringArrayListExtra(name, value);
intent.PutIntegerArrayListExtra(name, value);
2. 传递的是List<YourObject>
-
YourObject类必须要实现ISerializable或IParcelable接口 。
如果YourObject 是使用ISerializable实现的:
可以把list强转成Serializable类型,然后通过 PutExtras(key, (Serializable)list) 方法传递过去;
接收的时候用 (List<YourObject>)intent.GetSerializableExtra(key),就可以接收到List<YourObject>数据了。
如果YourObject 是使用IParcelable 实现的:
下面的例子为单个对象类的,不是List<>
发送如下
intent.PutExtra("device", (IParcelable)obj);
接收时候用
YourObject d = (YourObject)Intent.GetParcelableExtra("device");
本文详细介绍了在Android中如何使用Intent传递不同类型的参数,包括基本类型如String和Integer,以及List类型和自定义对象类型。对于自定义对象,需要实现ISerializable或IParcelable接口来完成传递。示例代码展示了具体的发送和接收方法。

2357

被折叠的 条评论
为什么被折叠?



