AndroidStudio中通过Activity传递数据

### Android学习笔记

分散传递

单个数据传递

在class1中写入需传递的数据

Intent intent = new Intent(this, MainActivity2.class);
intent.putExtra("userName", "小明");
startActivity(intent);

在class2中onCrtea方法中接受数据

Intent intent = getIntent();
if (intent != null) {
    String name =  intent.getStringExtra("userName");
    Log.d(TAG, "onCreate: ----name----"+ name);
}

多个数据传递

在Class1中传入需要的数据

Intent intent = new Intent(this, MainActivity2.class);
intent.putExtra("userName", "小明");
intent.putExtra("age", 22);
intent.putExtra("isLogin", false);
startActivity(intent);

在Class2中的onCreat方法中接受数据

Intent intent = getIntent();
if (intent != null) {
    String name =  intent.getStringExtra("userName");
    int age =  intent.getIntExtra("age",1);//默认值,当没有找到时候使用默认值
    boolean isLogin = intent.getBooleanExtra("isLogin", false);
    Log.d(TAG, "onCreate: --------"+ name);
    Log.d(TAG, "onCreate: --------"+ age);
    Log.d(TAG, "onCreate: --------"+ isLogin);
}

使用Bundle打包数据传递

传递单个Bundle

Intent intent = new Intent(this, MainActivity2.class);
Bundle bundle = new Bundle();
bundle.putString("name","小明");
bundle.putInt("age",24);
bundle.putBoolean("isLogin",false);
intent.putExtras(bundle);//要使用putExtras方法
startActivity(intent)

在Class2.onCrtea方法中接受数据

Intent intent = getIntent();
if (intent != null) {
    Bundle bundle = intent.getBundleExtras();//没有ky需要加s

    String name = bundle.getString("name");
    int age = bundle.getInt("age");
    boolean isLogin = bundle.getBoolean("islogin");

    Log.d(TAG, "onCreate: --------"+ name);
    Log.d(TAG, "onCreate: --------"+ age);
    Log.d(TAG, "onCreate: --------"+ isLogin);
}

传递多个Bundle数据

Intent intent = new Intent(this, MainActivity2.class);

Bundle bundle1 = new Bundle();
bundle1.putString("name","小明");
bundle1.putInt("age",24);
bundle1.putBoolean("isLogin",false);

Bundle bundle2 = new Bundle();
bundle2.putString("name","小红");
bundle2.putInt("age",22);
bundle2.putBoolean("isLogin",true);

intent.putExtra("bundle1",bundle1);//注意是ky
intent.putExtra("bundle2",bundle2);
startActivity(intent);

在Class2.onCrtea方法中接受数据

Intent intent = getIntent();
if (intent != null) {
    Bundle bundle1 = intent.getBundleExtra("bundle1");
    Bundle bundle2 = intent.getBundleExtra("bundle2");

    String name1 = bundle1.getString("name");
    int age1 = bundle1.getInt("age");
    boolean isLogin1 = bundle1.getBoolean("islogin");

    String name2 = bundle2.getString("name");
    int age2 = bundle2.getInt("age");
    boolean isLogin2 = bundle2.getBoolean("islogin");


    Log.d(TAG, "onCreate: --------"+ name1);
    Log.d(TAG, "onCreate: --------"+ age1);
    Log.d(TAG, "onCreate: --------"+ isLogin1);
    Log.d(TAG, "onCreate: --------"+ name2);
    Log.d(TAG, "onCreate: --------"+ age2);
    Log.d(TAG, "onCreate: --------"+ isLogin2);
}

传递JavaBean对象

首先JavaBean对象需要实现序列化接口Serializable

public class User implements Serializable {
    private String name;
    private int age;
    private boolean isLogin;
    // constructor and get and set and toString
}

在Class1中传入需要的类,可以传入一个也可以是多个

Intent intent = new Intent(this, MainActivity2.class);
User user1 = new User("小明",12, false);
User user2 = new User("小红", 23, true);
intent.putExtra("user1", (Serializable) user1);//序列化,需要该类需要实现Serializable接口才可以使用
intent.putExtra("user2", (Serializable) user2);

startActivity(intent);

在Class2.onCrtea方法中接受数据

Intent intent = getIntent();
if (intent != null) {
    User user1 = (User)intent.getSerializableExtra("user1");
    User user2 = (User)intent.getSerializableExtra("user2");

    System.out.println(user1);
    System.out.println(user2);
}

回传参数

在接受回传数据类class1,使用startActivityForResult进行跳转

public void test(View view) {
    Intent intent = new Intent(this, MainActivity2.class);
    startActivityForResult(intent,0);//requestCode
}

传递参数类class2

public void rutrunData(View view) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("name", "回传参数");
    setResult(1,intent);
    this.finish();//关闭页面

在class1中重写onActivityResult()方法

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode==0 && resultCode == 1 && data != null){
       String name =  data.getStringExtra("name");
        Log.d(TAG, "onActivityResult: ----------------"+ name);
    }
}

打印结果

点击返回还是返回按键都有效果

D/tag: onActivityResult: ----------------回传参数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值