1. 程式人生 > >intent傳遞物件,實現parceable或者serializable,及回傳值

intent傳遞物件,實現parceable或者serializable,及回傳值

intent是android四大元件互動的橋樑;

一個介面跳到另一個介面可以向其傳遞資訊引數,eg:資料基本型別,陣列,集合,bundle、物件。。

如果傳遞物件需要實現parceable介面或者serializable介面,實現parcelable效率比serializable高,

但是後者實現更簡單,更利於持久化儲存

package com.bdyl.constance;

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

/**
 * 圍欄資訊的類
 * 
 * @author Administrator
 * 
 */
public class Circle implements Parcelable {
	private String id;// 圍欄id
	private String device_id;// 裝置的ID
	private String circle_name;// 圍欄的名字
	private double circle_size;// 圍欄的範圍
	private double lng;// 圍欄中心位置的經度
	private double lat;// 圍欄中心位置的緯度
	private String type;//圍欄型別 0:safe 1:danger
	private String select;//0:選中顯示  1:不啟用
	private String position;//
	
	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	public String getSelect() {
		return select;
	}
	
	public void setSelect(String select) {
		this.select = select;
	}

	public String getCircle_name() {
		return circle_name;
	}

	public void setCircle_name(String circle_name) {
		this.circle_name = circle_name;
	}
	public String getDevice_id() {
		return device_id;
	}

	public void setDevice_id(String device_id) {
		this.device_id = device_id;
	}

	public double getCircle_size() {
		return circle_size;
	}

	public void setCircle_size(double circle_size) {
		this.circle_size = circle_size;
	}

	public double getLng() {
		return lng;
	}

	public void setLng(double lng) {
		this.lng = lng;
	}

	public double getLat() {
		return lat;
	}

	public void setLat(double lat) {
		this.lat = lat;
	}

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

	/*
	寫入
	*/
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(id);
		dest.writeString(circle_name);
		dest.writeDouble(circle_size);
		dest.writeDouble(lng);
		dest.writeDouble(lat);
		dest.writeString(device_id);
		dest.writeString(type);
		dest.writeString(select);
	}
	//固定格式,需要宣告為public static final ,物件名也必須是大寫的 CREATOR
	public static final Parcelable.Creator<Circle> CREATOR = new Parcelable.Creator<Circle>() {

	/*
	讀出
	*/
		@Override
		public Circle createFromParcel(Parcel source) {
			Circle circle = new Circle();
			circle.setId(source.readString());
			circle.setCircle_name(source.readString());
			circle.setCircle_size(source.readDouble());
			circle.setLng(source.readDouble());
			circle.setLat(source.readDouble());
			circle.setDevice_id(source.readString());
			circle.setType(source.readString());
			circle.setSelect(source.readString());
			return circle;
		}

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

	@Override
	public String toString() {
		return "Circle [id=" + id + ", device_id=" + device_id
				+ ", circle_name=" + circle_name + ", circle_size="
				+ circle_size + ", lng=" + lng + ", lat=" + lat + ", type="
				+ type + ", select=" + select + "]";
	}
	
	
	

}

關於回傳值:

tip:activity 的啟動模式不能為單任務singletask和單例項,值回傳不過去

activity1:

1.呼叫 startActivityForResult(iintent,requestcode);

2.重寫activity 的 onActivityForResult方法,

activity2:

getIntent().getEtras("key")..得到傳入的資料,

通過setResult(intent,resultCode); 將要回傳的資料通過intent

補充:

在fragment中的onActivityResult中處理回傳值,在activity中的此方法中呼叫 fragment.onActivityReusult()方法