Android使用Intent.putSerializable()进行数据传递,或者使用Bundle进行数据传递,实质上都是进行的Serializable数据的操作,说白了都是传递的原数据的一份拷贝,因此通过对象的传递来控制Android应用是不现实的

源代码如下了:

 1  import android.app.Activity;   2 import android.content.Intent;   3  import android.os.Bundle;   4  import android.util.Log;   5  import android.view.View;   6  import android.widget.Button;   7  import com.feng.androidbundle.activity.GetBundleActivity;   8  9  import com.feng.androidbundle.bean.DataBean;  10 11   12 public class AndroidbundleActivity extends Activity {  13     /** Called when the activity is first created. */  14     @Override  15     public void onCreate(Bundle savedInstanceState) {  16         super.onCreate(savedInstanceState);  17         setContentView(R.layout.main);  18           19         // 定义一个Bundle按键  20         Button gotoBundle = (Button)findViewById(R.id.btnBundle);  21         // 设置监听事件  22         gotoBundle.setOnClickListener(new View.OnClickListener(){  23   24             @Override  25             public void onClick(View v) {  26                 Intent intent = new Intent();  27                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  28                 intent.setClass(getApplicationContext(), GetBundleActivity.class);  29                   30                 DataBean temp  = new DataBean();  31                 temp.setID(1);  32                 temp.setName("曾驰文");  33                 temp.setAddress("xx市xx路xx号");  34                 temp.setMoblie("159xxxxxxxx");  35                   36 Log.i("AndroidbundleActivity", "新建时候的地址" + temp);     37                   38                 Bundle bundle = new Bundle();  39                 bundle.putSerializable("UserInfo", temp);  40                   41                 // 设置intent  42                 intent.putExtras(bundle);  43                   44                 // 发送Activity  45                 getApplicationContext().startActivity(intent);  46             }  47               48         });  49     }  50 }  

在另外一个Activity中将数据取出来 相应的源代码如下:

 1  import android.app.Activity;   2  import android.content.Intent;   3  import android.os.Bundle;   4  import android.util.Log;   5 import com.feng.androidbundle.R;   6  import com.feng.androidbundle.bean.DataBean;   7  public class GetBundleActivity extends Activity {   8  9       10     @Override  11     public void onCreate(Bundle savedInstanceState) {  12         // 初始化BundleActivity  13         super.onCreate(savedInstanceState);  14         setContentView(R.layout.bundle);  15           16         // 获取意图  17         Intent intent = getIntent();  18         Bundle bundle = intent.getExtras();  19           20         // 获取对象  21         DataBean temp = (DataBean) bundle.get("UserInfo");  22 Log.i("GetBundleActivity", "收到的地址" + temp);  23     }  24 }  

日志的打印结果

12-19 02:42:49.356: I/AndroidbundleActivity(230): 新建时候的地址com.zcw.androidbundle.bean.DataBean@44c24968
12-19 02:42:49.417: I/GetBundleActivity(230): 收到的地址com.zcw.androidbundle.bean.DataBean@44c33528

由上面的日志可以看出,使用Bundle进行数据的传递,实现了serialzable接口实质上是拷贝的传递。

更多相关文章

  1. 使用SharedPreferences存储和读取数据
  2. Android:Content Provider数据共享
  3. android中usb数据通信速率慢问题解决办法
  4. Android sqlite 数据库操作
  5. [置顶] Android SDK中的例子的源代码
  6. Android 自定义Listview 如何绑定Sqlite数据库数据
  7. Android的数据存储
  8. Android APN的设置问题 默认“已起用数据” 关闭

随机推荐

  1. Android与J2ME区别之我见(3)
  2. (推荐)彻底解析Android缓存机制——LruCach
  3. 写给Android开发者的Kotlin入门
  4. Android公共库选型 单元测试 依赖管理等
  5. Android之Handle全面理解
  6. Android使用Intent Filter来响应隐式Inte
  7. 5、控件系列之TOAST(吐司提示)的曾经、现在
  8. ADB WIFI DEBUG(ANDROID STUDIO)
  9. Android闹钟设置的解决方案
  10. 微软之鉴:Google不会将Android与Chrome OS