最简单的,主要是实现Parcelable的接口,重写那个CREATOR
测试用的接收信息Activity
package com.nico;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
public class Test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = getIntent();
Person p = i.getParcelableExtra("yes");
System.out.println("---->"+p.name);
System.out.println("---->"+p.map.size());
}
}
发送的Activity
package com.nico;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class TestNew extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent();
Person p = new Person();
p.map = new HashMap<String,String>();
p.map.put("yes", "ido");
p.name="ok";
intent.putExtra("yes", p);
intent.setClass(this, Test.class);
startActivity(intent);
}
}
Parcelable的实现类
package com.nico;
import java.util.HashMap;
import android.os.Parcel;
import android.os.Parcelable;
public class Person implements Parcelable {
public HashMap<String,String> map = new HashMap<String,String> ();
public String name ;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeMap(map);
dest.writeString(name);
}
public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
@Override
public Person createFromParcel(Parcel source) {
Person p = new Person();
p.map=source.readHashMap(HashMap.class.getClassLoader());
p.name=source.readString();
return p;
}
@Override
public Person[] newArray(int size) {
// TODO Auto-generated method stub
return null;
}
};
}