【转】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种接口中的一种 ...
随机推荐
- ASP.NET MVC5总结(一)@HTML和对应的HTML
HtmlHelper用来在视图中呈现 HTML 控件,主要分为以下几类: 1.ActionLink - 链接到操作方法 @Html.ActionLink("这是一个连接", &qu ...
- asp.net:用类来后台绑定数据源
//封装成一个 using System;using System.Collections.Generic;using System.Linq;using System.Web;using Syste ...
- java新手笔记1 Hello World!
//Hello.java文件 //类声明 public class Hello{ //声明方法 main程序入口 public static void main (String[] args) { S ...
- Ambiguous handler methods mapped for HTTP path
一.问题:映射重复导致的错误 java代码如下: @RequestMapping(value = "/info/{remove}/{id}", method = RequestMe ...
- Codevs 5208 求乘方取模
5208 求乘方取模 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 未定级 题目描述 Description 给定非负整数A.B.M,求(A ^ B) mod M. 输入描述 Inpu ...
- HttpUtility.HtmlEncode
HttpUtility.HtmlEncode用来防止站点受到恶意脚本注入的攻击 public string Welcome(string name, int numTimes = 1) { r ...
- 安装hadoop多节点 各种整理
ubuntu烧制usb启动盘链接: 点击打开链接https://help.ubuntu.com/community/Installation/FromUSBStick ubuntu磁盘分区: 点击打开 ...
- IE6 png 透明 (三种解决方法)
FF和IE7已经直接支持透明的png图了,下面这个主要是解决IE6下透明PNG图片有灰底的 ====================================================== ...
- session 保存在指定的数据表,cookie设置
首先建立数据表,可在ThinkPHP/Extend/Driver/Session/SessionDb.class.php中copy代码 在配置文件中配置: 'SESSION_TYPE' => ' ...
- java中运算符——进度1
Class Demo1{ public static void main(String[] args) { /* 一.逻辑运算法用于连接两个boolean类型的表达式 ...