zoukankan      html  css  js  c++  java
  • android Acitivity之间的几种传值方式(^_^)

     对于开发app 来说,数据的传递肯定是少不了的啦,其实app 的本质就是用来呈现数据的。

     好的

     方式一  Intent.putExtra(TAG,DATA);

          应用场景   对于传送单一数据,而又只在两个Activity之间传送的的。

          发出 sendActivity

                      Intent intent=new Intent(sendActivity.this,receiveActivity.class);

                      intent.putExtra(TAG,DATA);

                      startActivity(intent);

          接收 receiveActivity

                       在receiveActivity 的onCreate() 方法下

                      String name=getIntent().getIntExtra(TAG);

           注:TAG  是一个String 的标志

                 DATA 是数据  可以是String ,int,bool等。

     

      方式二  Intent.putExtras(Bundle);

          应用场景  对与多个数据 而又只在两个Activity之间传送的的。

          发出 sendActivity

                Intent intent=new Intent(sendActivity.this,receiveActivity.class);

                Bundle bundle=new Bundle();

                bundle.putString("数据一", 数据一);

                bundle.putString("数据二", 数据二);

                bundle.putString("数据三", 数据三);

                。。。。

                intent.putExtras(bundle);

                startActivity(intent);

          接收 receiveActivity

              数据一=this.getIntent().getExtras().getString("  数据一");

              数据二=this.getIntent().getExtras().getString("  数据二");

              数据三=this.getIntent().getExtras().getString("  数据三");

              。。。。

     

           注 Bundle是一个封装数据的对象 ,不知道你们是怎么理解,我就是这样理解的可以封装很多类型的。

     方式 三 Application       

            应用场景     对与多个数据 或 但单个数据都可以 根据自己需求定义

            需要到AndroidManifest.xml  的  applicaion 节点 的android:name 属性加上 Application类名。

            

             

     

  • 相关阅读:
    python format() 函数
    -bash: fork: Cannot allocate memory 问题的处理
    阿里云telnet 3306端口失败
    npm install报错 npm ERR! enoent ENOENT: no such file or directory
    springboot启动后总是自己shutdown
    thymeleaf给bootstrap自定义变量赋值
    java通过反射拷贝两个对象的同名同类型变量
    使用awk按照行数切割文件
    Iterable接口
    mac brew update 报错
  • 原文地址:https://www.cnblogs.com/spyrx7/p/4291962.html
Copyright © 2011-2022 走看看