zoukankan      html  css  js  c++  java
  • Android学习之Activity跳转与传值

    Activity跳转与传值。主要是通过Intent类。Intent的作用是激活组件和附带数据。

     


    一、Activity跳转

    方法一
    Intent intent = new Intent(A.this, B.class); 
    startActivity(intent)

     

    方法二
    Intent intent = new Intent();
    intent.setClass(A.this, B.class);
    startActivity(intent);

    实现从A跳转到B(A、B均继承自Activity)

     

     

    二、传递数据

    Activity A 传递数据

    方法一
    Intent intent = new Intent();
    intent.setClass(A.this, B.class);
    intent.putExtra("name", "xy");
    intent.putExtra("age", 22);

    startActivity(intent);

     

    方法二
    Intent intent = new Intent(A.this, B.class); 
    Bundle bundle = new Bundle();
    bundle.putString("name", "xy");
    bundle.putInt("age", 22);

    intent.putExtras(bundle);
    startActivity(intent);

     


    Activity B 接收数据


    // 获取參数1
    Intent intent = this.getIntent();
    String name = intent.getStringExtra("name");
    int age = intent.getIntExtra("age", 22); // 缺省值为22

    // 获取參数2
    Bundle bundle = intent.getExtras();
    String name2 = bundle.getString("name");
    int age2 = bundle.getInt("age", 22);

    两种获取參数方式均可,并非和传參1,2方法一一相应

  • 相关阅读:
    刨析js代码执行机制
    H5离线缓存基础系列
    meta 详解
    如何成长为一名合格的web架构师?
    整理的互联网公司面试趋势
    http协议
    前端现在到底需要什么样的人才
    webpack 4.0 版本的简单使用
    vue的懒加载如何实现?
    Runtime的几个小例子(含Demo)
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/6900207.html
Copyright © 2011-2022 走看看