send objects through bundle
来源:互联网 发布:牧场数据信息管理平台 编辑:程序博客网 时间:2024/06/08 12:48
One More way to send objects through bundle is by using bundle.putByteArray
Sample code
public class DataBean implements Serializable {private Date currentTime;public setDate() { currentTime = Calendar.getInstance().getTime(); }public Date getCurrentTime() { return currentTime; }}
put Object of DataBean in to Bundle:
class FirstClass{public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//Your code...//When you want to start new Activity...Intent dataIntent =new Intent(FirstClass.this, SecondClass.class); Bundle dataBundle=new Bundle(); DataBean dataObj=new DataBean(); dataObj.setDate(); try { dataBundle.putByteArray("Obj_byte_array", object2Bytes(dataObj)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } dataIntent.putExtras(dataBundle); startActivity(dataIntent);}
Converting objects to byte arrays
/** * Converting objects to byte arrays */static public byte[] object2Bytes( Object o ) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream( baos ); oos.writeObject( o ); return baos.toByteArray(); }
Get Object back from Bundle:
class SecondClass{DataBean dataBean;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//Your code...//Get Info from Bundle... Bundle infoBundle=getIntent().getExtras(); try { dataBean = (DataBean)bytes2Object(infoBundle.getByteArray("Obj_byte_array")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }}
Method to get objects from byte arrays:
/** * Converting byte arrays to objects */static public Object bytes2Object( byte raw[] ) throws IOException, ClassNotFoundException { ByteArrayInputStream bais = new ByteArrayInputStream( raw ); ObjectInputStream ois = new ObjectInputStream( bais ); Object o = ois.readObject(); return o; }
Hope this will help to other buddies.
0 0
- send objects through bundle
- Send email through server agent
- Send "aaa" to NotePad through handle
- Cannot send events to objects owned by a different thread
- 【python】RuntimeError: Lock objects should only be shared between processes through inheritance
- cannot pass objects of non-trivially-copyable type 'std::string ...' through '...'
- how to send blogs to wordpress through the windows live writer
- How to send Email through external SMTP server on Ubuntu 14.04
- ASSERT “Cannot send events to objects owned by a different thread.” SoQt Coin Qt
- Send and Receive JSON objects to Web Service Methods using jQuery AJAX in ASP.Net
- send
- send
- send
- Send
- Bundle
- Bundle
- Bundle
- Bundle
- IOS开发日志记录 - 尺寸
- Qt开发环境搭建
- 配置fastDFS 一个tracker管理多个Storage Group
- android获取SDCard中照片并按照时间顺序显示
- IOS开发之----Xcode非ARC项目中设置部分文件ARC支持
- send objects through bundle
- ubuntu中minicom的安装
- java 反射相关
- 排序算法(二):冒泡排序
- C#向C++封送结构体数组
- Android 新浪授权和分享经验总结
- matlab编程入门学习(3)
- gif和jpg图片比较
- 【redis】常见JedisConnectionException异常分析