Activity之间互相发送数据

activity_send_data_req.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <Button
        android:id="@+id/btn_send_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/send_data" />
</LinearLayout>

SendDataReqActivity.java

package com.example.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

public class SendDataReqActivity extends AppCompatActivity {
    private ActivityResultLauncher<Intent> register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_data_req);

        // 接收返回数据:注册 ActivityResultLauncher
        register = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (result.getResultCode() == Activity.RESULT_OK) {
                        Intent data = result.getData();
                        if (data != null) {
                            String returnedData = data.getStringExtra("resultKey");
                            Toast.makeText(SendDataReqActivity.this, "响应数据: " + returnedData, Toast.LENGTH_SHORT).show();
                        }
                    }
                }
        );

        // 按钮事件
        findViewById(R.id.btn_send_data).setOnClickListener(v -> {
            Intent intent = new Intent(this, SendDataRespActivity.class);
            // 创建一个包裹
            Bundle bundle = new Bundle();
            bundle.putString("dataKey", "你好!!!");
            intent.putExtras(bundle);
            register.launch(intent);
        });
    }
}

activity_send_data_resp.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/receive_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" />

    <Button
        android:id="@+id/btn_return_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/return_data" />
</LinearLayout>

SendDataRespActivity.java

package com.example.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class SendDataRespActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_data_resp);

        // 获取传递过来的数据
        Intent intent = getIntent();
        String receivedData;
        if (intent != null) {
            receivedData = intent.getStringExtra("dataKey");
            if (receivedData != null) {
                // 将数据显示在页面上
                TextView textView = findViewById(R.id.receive_data);
                String show = "接收的数据:" + receivedData;
                textView.setText(show);
            }
        } else {
            receivedData = "";
        }

        // 启动 SecondActivity
        findViewById(R.id.btn_return_data).setOnClickListener(v -> {
            Intent returnIntent = new Intent(SendDataRespActivity.this, SendDataReqActivity.class);
            returnIntent.putExtra("resultKey", "这是你传给我的数据:" + receivedData);
            setResult(Activity.RESULT_OK, returnIntent);
            finish();
        });
    }
}

AndroidManifest.xml 加上以下内容

	<activity
            android:name=".SendDataReqActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SendDataRespActivity"
            android:exported="false">
        </activity>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值