【转】Android中intent传递对象和Bundle的用法
原文网址:http://blog.csdn.net/lixiang0522/article/details/8642202
- package com.hebaijun.testparcelable;
- import android.os.Parcel;
- import android.os.Parcelable;
- public class ParcelableData implements Parcelable{
- private String name;
- private int age;
- public ParcelableData(){
- name = "guest";
- age = 20;
- }
- public ParcelableData(Parcel in){
- //顺序要和writeToParcel写的顺序一样
- name = in.readString();
- age = in.readInt();
- }
- public String getName(){
- return name;
- }
- public void setName(String name){
- this.name = name;
- }
- public int getAge(){
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public int describeContents() {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public void writeToParcel(Parcel dest, int flags) {
- // TODO Auto-generated method stub
- dest.writeString(name);
- dest.writeInt(age);
- }
- public static final Parcelable.Creator<ParcelableData> CREATOR = new Parcelable.Creator<ParcelableData>() {
- public ParcelableData createFromParcel(Parcel in) {
- return new ParcelableData(in);
- }
- public ParcelableData[] newArray(int size) {
- return new ParcelableData[size];
- }
- };
- }
- Intent intent = new Intent();
- intent.setClass(this, SubActivity.class);
- // 直接添加
- //intent.putExtra("MyData", new ParcelableData());
- // 通过Bundle
- Bundle bundle = new Bundle();
- bundle.putString("MyString", "test bundle");
- bundle.putParcelable("MyData", new ParcelableData());
- intent.putExtras(bundle);
- startActivity(intent);
- //ParcelableData parcelableData = getIntent().getParcelableExtra("MyData");
- Bundle bundle = getIntent().getExtras();
- ParcelableData parcelableData = bundle.getParcelable("MyData");
- String testBundleString = bundle.getString("MyString");
- Log.v("string=", testBundleString);
- Log.v("name=", parcelableData.getName());
- Log.v("age=", ""+parcelableData.getAge());
传输的对象需要实现序列化:有两种方式,一种是实现Serializable接口,就是原来的java方式;另外一种是android的Parcelable方式,这个性能可能好一些,我猜的,但是这在需要手动去写Parcelable接口的实现。
Serializable存数据:
- Person mPerson = new Person();
- mPerson.setName("frankie");
- mPerson.setAge(25);
- Intent mIntent = new Intent(this,ObjectTranDemo1.class);
- Bundle mBundle = new Bundle();
- mBundle.putSerializable(SER_KEY,mPerson);
- mIntent.putExtras(mBundle);
Serializable取数据:
// 获取启动该ResultActivity的Intent |
24 |
Intent intent = getIntent(); |
25 |
// 获取该Intent所携带的数据 |
26 |
Bundle bundle = intent.getExtras(); |
27 |
// 从bundle数据包中取出数据 |
28 |
Person person = (Person) bundle.getSerializable( "person" ); |
Parcelable存数据:
- Intent mIntent = new Intent(this,ObjectTranDemo2.class);
- Bundle mBundle = new Bundle();
- mBundle.putParcelable(PAR_KEY, mBook);
- mIntent.putExtras(mBundle);
Parcelable取数据:
- Book mBook = (Book)getIntent().getParcelableExtra(ObjectTranDemo.PAR_KEY);
参考1:http://blog.csdn.net/Android_Tutor/article/details/5740845
【转】Android中intent传递对象和Bundle的用法的更多相关文章
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putP ...
- [转]Android中Intent传递对象的两种方法(Serializable,Parcelable)
http://blog.csdn.net/xyz_lmn/article/details/5908355 今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种 ...
- Android高手进阶教程(十七)之---Android中Intent传递对象的两种方法(Serializable,Parcelable)!
[转][原文] 大家好,好久不见,今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object); ...
- android#使用Intent传递对象
参考自<第一行代码>——郭霖 Intent的用法相信你已经比较熟悉了,我们可以借助它来启动活动.发送广播.启动服务等.在进行上述操作的时候,我们还可以在Intent中添加一些附加数据,以达 ...
- Android中Intent传递类对象的方法一(Serializable)
Activity之间通过Intent传递值,支持基本数据类型和String对象及它们的数组对象byte.byte[].char.char[].boolean.boolean[].short.short ...
- Android通过Intent传递对象
1.传递Serializable方式类对象 首先创建一个序列化类:User import java.io.Serializable; public class User implements Seri ...
- Intent传递对象的几种方式
原创文章.转载请注明 http://blog.csdn.net/leejizhou/article/details/51105060 李济洲的博客 Intent的使用方法相信你已经比較熟悉了,Inte ...
- Android 通过 Intent 传递类对象或list对象
(转:http://www.cnblogs.com/shaocm/archive/2013/01/08/2851248.html) Android中Intent传递类对象提供了两种方式一种是 通过实现 ...
- Android 通过 Intent 传递类对象
Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种 ...
随机推荐
- 经历:easyui的datagrid没有数据滚动条的显示
今天,一个用户提出一个这样的问题,"查询不到结果时,为什么我看不到后面的标题呢?" 最初,我听到这个问题时,第一反应是:查出来数据不就有滚动条了吗,干嘛非要较真呢? 不过,后来想想 ...
- ios UIWebView截获html并修改便签内容
需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果: 方法:通过js截获UIWebView中的html,然后修改html标签内容: 实例代码: 服务器端 ...
- 控制器的跳转-modal与push
一.modal与pushmodal从下面往上盖住原来的控制器,一般上一个控制器和下一个控制器没有什么关联时用modal,比如联系人的加号跳转页面,任何控制器都可以用modal push一般是上下文有关 ...
- UVA 10795 A Different Task(汉诺塔 递归))
A Different Task The (Three peg) Tower of Hanoi problem is a popular one in computer science. Briefl ...
- VIM 及正则表达式
VIM及正则表达式 一.查找/Search + 统计 1.统计某个关键字 方法是:%s:keyword:&:gn. 其中,keyword是要搜索的关键字,&表示前面匹配的字符串,n表示 ...
- PAT_1026 程序运行时间
问题描述: 要获得一个C语言程序的运行时间,常用的方法是调用头文件time.h,其中提供了clock()函数,可以捕捉从程序开始运行到clock()被调用时所耗费的时间.这个时间单位是clock ti ...
- MATLAB【工具箱下载】汇总
至于工具箱的安装说明参见:http://www.matlabsky.com/thread-120-1-1.html Maplesoft<Maple Toolbox for MATLAB> ...
- 【ADO.NET】5、手机归属地查询( winfrom )
using System.IO; 有一个数据库手机号码的txt文件,格式是 : 13500000000-13560000000-中国移动 查询结果: 湖南移动[邵阳]文件夹选择对话框 FolderBr ...
- js 中如何通过提示框跳转页面
通过提示框跳转页面 <!doctype html> <html lang="en"> <head> <meta charset=" ...
- php异步请求(可以做伪线程)
$fp = fsockopen("www.baidu.com", 80, $errno, $errstr, 30); stream_set_blocking($fp,0); ...