- Сначала сериализуйте объект, который вы хотите передать.
- Поместите объект сериализации в дополнительные функции.
- При получении, просто получите сериализованный объект, десериализовайте его.
скажем,
Employee employee = new Employee();
затем,
intent.putExtra("employee", serializeObject(employee));
при получении,
byte[] sEmployee = extras.getByteArray("employee");
employee = (Employee) deserializeObject (sEmployee));
К вашему сведению,
public static byte[] serializeObject(Object o) throws Exception,IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos);
try {
out.writeObject(o);
// Get the bytes of the serialized object
byte[] buf = bos.toByteArray();
return buf;
} catch (IOException e) {
Log.e(LOG_TAG, "serializeObject", e);
throw new Exception(e);
} finally {
if (out != null) {
out.close();
}
}
}
public static Object deserializeObject(byte[] b)
throws StreamCorruptedException, IOException,
ClassNotFoundException, Exception {
ObjectInputStream in = new ObjectInputStream(
new ByteArrayInputStream(b));
try {
Object object = in.readObject();
return object;
} catch (Exception e) {
Log.e(LOG_TAG, "deserializeObject", e);
throw new Exception(e);
} finally {
if (in != null) {
in.close();
}
}
}