activity跳转时携带数据:

来源:互联网 发布:一氧化二氮 淘宝 编辑:程序博客网 时间:2024/04/28 16:08

activity跳转时携带数据:

1.使用intent的意图

Intent intent = new Intent(this,SecondActivity.class);//把数据封装到intent对象中intent.putExtra("Name",Name);//获取启动此Activity的意图Intent intent = getIntent();String name = intent.getStringExtra("Name");

2.使用bundle,bundle相当于map集合。

//先把数据封装到bundleBundle bundle = new Bundle();bundle.putString("Name",Name);//再把bundle封装至intentintent.putExtras(bundle);Bundle bundle = intent.getExtras();String Name = bundle.getString("Name");

3.总结这两种方式

Activity跳转时携带数据

  • 跳转时携带数据都是把数据封装至intent对象中
  • intent能封装的数据
    • 八大基本数据类型及其数组
    • 字符串及其数组
    • Bundle
    • 实现了序列化接口的对象
      • Parcelable:Android提供的,内存中传输对象使用此接口
      • Serializable:java提供的,本地持久化保存对象以及网络传输对象使用此接口
  • Bundle能封装的数据
    • intent能存的Bundle也能存
    • Ibinder
1 0
原创粉丝点击