• Android中2个activity之间的数据传递方法


    1. 通过intent来传递:

    activity1中设置

          A.传字符等:

      Java代码

      String text = "hello";

      Intent intent1 = new Intent(ActivityMain.this, Activity2.class);
      intent1.putExtra("activity1", text);

      startActivity(intent1);

          B. 传对象,对象要实例化,继承Serializable

      Java代码

      Bundle mbundle=new Bundle();
        mbundle.putSerializable("user",userList.get(position));

      Intent in =new Intent (getApplicationContext(), activity2.class);
      in.putExtras(mbundle);

      startActivity(in);

    activity2中接收:

          A:接收

      Java代码

      Bundle extras = getIntent().getExtras();

      if (extras != null) {
          textview.setText(extras.getString("activity1"));
      }

      B.接收

      Java代码

      Bundle bundel = getIntent().getExtras();
      user= (User) bundel.get("user");

    2. SharedPreferences

    在activity1中设置的如下:

      Java代码

      SharedPreferences sp = getSharedPreferences("textinfo",0);
      Editor editor = sp.edit();
      String text = "hello";

      editor.putString("text", text);
      editor.commit();

        Intent i = new Intent(getApplicationContext(),activity2.class);

      startActivity(i);

      跳转到Message的activity,获取内容如下

      Java代码

      SharedPreferences share = getSharedPreferences("textinfo",0);
      String text = share.getString("text", null);
      msgtextview.setText(text);

  • 相关阅读:
    Azure的CentOS上安装LIS (Linux Integration Service)
    使用PowerShell在Azure China创建Data Warehouse
    通过php的MongoDB driver连接Azure的DocumentDB PaaS
    Azure RBAC管理ASM资源
    Azure基于角色的用户接入控制(RBAC)
    通过PowerShell命令给Azure VM添加CustomScriptExtension
    手把手教你创建Azure ARM Template
    MySQL数据表列转行
    MySQL
    MySQL游标使用
  • 原文地址:https://www.cnblogs.com/eustoma/p/2415873.html
Copyright © 2020-2023  润新知