Android开发:Bundle传值。之前使用的是Intent传值,新学了一个Bundle传值的方法

文章目录

  • 一、Bundle是什么?
  • 二、Bundle与Intent区别
  • 三、Bundle使用
    • 代码示例
    • 效果显示

一、Bundle是什么?

Android Developers 文档【Bundle】

public final class Bundle extends BaseBundle implements Cloneable, Parcelable

A mapping from String keys to various Parcelable values. 翻译:从 String 键到各种 Parcelable 值的映射。

Bundle类是一个final类,是一个存储和管理key-value对的类,主要用于存储并传递数据。

二、Bundle与Intent区别

Android Developers 文档【Intent】

An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity. 翻译:Intent 是对要执行的操作的抽象描述。 它可以与 startActivity 一起使用来启动一个 Activity

Bundle与Intent都可以用来传值,Bundle可以对对象进行操作,而Intent是不可以。Bundle相对于Intent拥有更多的接口,用起来比较灵活,但是使用Bundle也还是需要借助Intent才可以完成数据传递总之,Bundle旨在存储数据,而Intent旨在传值。

三、Bundle使用

Main1.java 代码中的这句:

Intent intent = new Intent().setClassName("com.pkg.myadapter", "com.pkg.myadapter.Main2");

com.pkg.myadapter为收数据页面的包名,com.pkg.myadapter.Main2为收数据页面的类名。就是页面跳转,可替换成:

Intent intent = new Intent(Main1.this,Main2.class);

代码示例

发数据: Main1.java

public class Main1 extends AppCompatActivity {private Button button1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main1);//添加按键跳转到第二个页面Main2.javabutton1=findViewById(R.id.button1);button1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent intent = new Intent().setClassName("com.pkg.myadapter", "com.pkg.myadapter.Main2");Bundle bundle = new Bundle();bundle.putString("name", "zhangsan");bundle.putInt("age", 18);intent.putExtras(bundle);startActivity(intent);finish();}});
}

收数据:Main2.java

public class Main2 extends AppCompatActivity {private Button button3;private TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2);Intent intent1=getIntent();// 实例化BundleBundle bundle=intent1.getExtras();String name=bundle.getString("name");int age=bundle.getInt("age");TextView dataTextView = (TextView) findViewById(R.id.textView2); //文本控件接收数据dataTextView.setText("姓名:"+name+"年龄:"+age);button3=findViewById(R.id.button3);button3.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent intent2 = new Intent(Main2.this,Main1.class);        //重新跳回Main1startActivity(intent1);      //激活活动显示接收的数据startActivity(intent2);        //重新跳回Main1finish();       //结束当前Activity}});}}

activity_main1.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".Main1"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:layout_editor_absoluteX="16dp"tools:layout_editor_absoluteY="0dp"><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="This is Main1……"android:textSize="30sp" /><Buttonandroid:id="@+id/button1"android:layout_width="105dp"android:layout_height="wrap_content"android:layout_gravity="center"android:text="点击跳转" /></LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".Main2"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:layout_editor_absoluteX="0dp"tools:layout_editor_absoluteY="0dp"><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="This is Main2……"android:textSize="30sp" /><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="返回" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView"android:layout_gravity="center"android:textSize="30sp"/></LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>

效果显示

Android开发:Bundle传值相关推荐

  1. Android开发中使用Bundle数据传值

    Bundle是Android开发中的一个类,用于Activity之间传输数据用,Bundle就是一个专门用于导入Intent传值的包. 1.MainActivity.xml 传输数据(4步) //设置 ...

  2. android viewpager fragment传值,Android开发中如何解决Fragment +Viewpager滑动页面重复加载的问题...

    前言 之前在做一个Viewpager上面加载多个Fragment时总会实例化已经创建好的Fragmnet对象类似 viewPager.setAdapter(new FragmentPagerAdapt ...

  3. Android开发——进程间通信之Bundle和文件

    0.  前言 不论是Android还是其他操作系统,都会有自己的IPC机制,所谓IPC(Inter-Process Communication)即进程间通信.首先线程和进程是很不同的概念,线程是CPU ...

  4. 分享篇 - 58同城基于Android APP Bundle开发的全新编译模式(编译速度提升70%)

    目录 1. Wafers 项目背景 2. 效果展示 3. 实现方案 4. 改造期间遇到的问题 5. 如何接入使用 6. 对比 Instant Run 和 Apply Changes 7. 总结 1. ...

  5. android bundle 机制,【Android开发】Bundle机制详解

    在同一个地方跌倒两次,才能体会到"好记性不如烂笔头"! 一.Bundle简介 bundle在Android开发中非常常见,它的作用主要时用于传递数据:它所保存的数据是以key-va ...

  6. Android开发之手机拍照功能的实现(源代码分享)

    Android系统里调用手机拍照的功能有两种方法一种直接调用手机自带的相机另一种就是使用自己做的拍照应用.比如Camera360 一款于各操作系统都有的的功能强大的手机摄影软件:能拍摄出不同风格,不同 ...

  7. Android开发RSS阅读器

    RSS阅读器的Logo: RSS阅读器是一种软件或是说一个程序,这种软件可以自由读取RSS和Atom两种规范格式的文档,且这种读取RSS和Atom文档的软件有多个版本,由不同的人或公司开发,有着不同的 ...

  8. .Net程序猿玩转Android开发---(11)页面跳转

    在不论什么程序开发中,都会遇到页面之间跳转的情况,Android开发也不例外.这一节,我们来认识下Android项目中如何进行页面跳转.页面跳转分为有參数和无參数页面跳转,已经接受还有一个页面的返回值 ...

  9. Android开发本地及网络Mp3音乐播放器(二十)歌曲下载完成后通知主界面更新本地音乐

    转载请注明出处:http://blog.csdn.net/iwanghang/article/details/51448597 项目源码(打赏5积分请点这边):http://download.csdn ...

最新文章

  1. 数据结构与算法-二叉树(java描述)
  2. 华为nova好不好 先看图
  3. 基于android公交车线路查询论文文献,本科毕业论文---基于android的手机公交线路查询系统.doc...
  4. LeetCode 1976. 到达目的地的方案数(迪杰斯特拉 Python 优先队列)
  5. 自动化代码部署、代码回滚、命令执行软件之capistrano
  6. no talloc stackframe at ../source3/param/loadparm.c:4864, leaking memory
  7. java 数据库连接 释放_JDBC连接数据库和释放连接
  8. 关于swf转fla 软件的间题
  9. 怎么把手机字体改成繁体_如何把手机字体变成繁体 繁体字转换器
  10. javascript 字符串的排列与组合
  11. poi html转换成word文档,Apache POI将HTML转换成Word
  12. Cmd 移动文件夹及文件
  13. 李沐笔记(softmax回归)
  14. 极线约束(epipolar constraint)
  15. [转载].NET商业软件源码保护
  16. 4.7 matlab交互式绘图工具(绘图工具栏)
  17. 【ACWing】195. 骑士精神
  18. Git版本控制工具的使用一
  19. 自协方差函数,自相关函数,协方差矩阵
  20. 数据结构——非线性结构(树与二叉树)

热门文章

  1. luckySheet实现Excel在线保存,导出,编辑
  2. 登录页面-->网页设计
  3. 开机动画默认横屏,开机默认横屏,修改底部虚拟按键方向位置,主屏幕可旋转
  4. Faster-rcnn环境搭建与训练自己的数据
  5. 来点干货,怎么面国企或银行?终于写完了
  6. java linux路径写法_window linux 路径写法(转载)
  7. 同时学cpa和Java_CPA可以和哪些证书同时备考?不知道你就OUT了
  8. fedora安装vim可用
  9. Python的特殊属性和特殊方法
  10. DREP与陀螺传媒强强联合,将基于DREP SDK与陀螺平台打造超级链游