码迷,mamicode.com
首页 > 移动开发 > 详细

Android_Parcelable

时间:2014-09-17 12:13:12      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:android   parcelable   

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Intent intent = getIntent();
		if (intent != null) {
			Parcelable p = intent.getParcelableExtra("info");
			if (p != null) {
				System.out.println(p);
			}
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

	}

	public void start(View view) {
		Intent intent = new Intent(getApplicationContext(), MainActivity.class);
		intent.putExtra("info", new Bean(100, "baidu"));
		startActivity(intent);
	}

}
import android.os.Parcel;
import android.os.Parcelable;

public class Bean implements Parcelable {
	int age;
	String name;

	public Bean(int age, String name) {
		this.age = age;
		this.name = name;
	}

	@Override
	public int describeContents() {
		//
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		//向dest容器中写入数据,int占4个字节,至于区分String应该有特殊的标志位,相当于反序列化
		dest.writeInt(this.age);
		dest.writeString(this.name);
	}

	public static final Parcelable.Creator<Bean> CREATOR = new Parcelable.Creator<Bean>() {
		@Override
		public Bean createFromParcel(Parcel source) {
			//按照顺序从source中解析出数据,相当于序列化
			int age = source.readInt();
			String name = source.readString();
			return new Bean(age, name);
		}

		@Override
		public Bean[] newArray(int size) {
			return new Bean[size];
		}
	};

	@Override
	public String toString() {
		return "Bean [age=" + age + ", name=" + name + "]";
	}
	
}


Android_Parcelable

标签:android   parcelable   

原文地址:http://blog.csdn.net/zimo2013/article/details/39339033

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!