安卓开发学习-向上一个Activity返回数据

发送请求页面

点击查看代码
package com.android.response;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

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

public class RequestActivity extends AppCompatActivity {

    private ActivityResultLauncher<Intent> launcher;

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

        // 获取布局中的 TextView 和 Button
        TextView data = findViewById(R.id.tv_data);
        Button sendBtn = findViewById(R.id.btn_send);

        // 初始化 ActivityResultLauncher
        launcher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    // 检查活动结果
                    if (result.getResultCode() == RESULT_OK) {
                        Intent dataIntent = result.getData();
                        if (dataIntent != null) {
                            Bundle extras = dataIntent.getExtras();
                            if (extras != null) {
                                // 从返回的数据中提取信息
                                String receivedData = extras.getString("backData");
                                // 在 TextView 中显示接收到的数据
                                TextView receiveTv = findViewById(R.id.tv_back_data);
                                receiveTv.setText(receivedData);
                            }
                        }
                    }
                });

        // 设置按钮的点击事件监听器
        sendBtn.setOnClickListener(e -> {
            // 创建一个 Bundle 对象用于传递数据
            Bundle bundle = new Bundle();
            // 从 TextView 中提取数据,并将其放入 Bundle 中
            bundle.putString("data", data.getText().toString().split(":")[1]);

            // 创建一个 Intent 对象,并将 Bundle 放入其中
            Intent intent = new Intent(this, ResponseActivity.class);
            intent.putExtras(bundle);

            // 使用 ActivityResultLauncher 启动活动
            launcher.launch(intent);
        });
    }
}

接收返回请求界面

点击查看代码
package com.android.response;

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

import androidx.appcompat.app.AppCompatActivity;

public class ResponseActivity extends AppCompatActivity {

    // 预设的返回数据
    private static final String backData = "你好未来!";

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

        // 获取布局中的 TextView
        TextView receiveTv = findViewById(R.id.tv_receive);
        // 获取从启动 Intent 中传递过来的数据
        Bundle extras = getIntent().getExtras();

        // 检查是否有传递过来的数据
        if (extras != null) {
            // 从 Bundle 中提取数据
            String receivedData = extras.getString("data");
            // 在 TextView 中显示接收到的数据
            receiveTv.setText(receivedData);
        }

        // 获取布局中的返回按钮
        Button backBtn = findViewById(R.id.btn_back);
        // 设置返回按钮的点击事件监听器
        backBtn.setOnClickListener(e -> {
            // 创建一个新的 Intent
            Intent intent = new Intent();
            // 创建一个 Bundle 对象用于存储返回的数据
            Bundle bundle = new Bundle();
            // 将预设的返回数据放入 Bundle
            bundle.putString("backData", backData);
            // 将 Bundle 放入 Intent
            intent.putExtras(bundle);
            // 设置返回结果为成功,并将带有数据的 Intent 返回给上一个活动
            setResult(Activity.RESULT_OK, intent);
            // 结束当前活动
            finish();
        });
    }
}

image
image
image

posted on 2024-03-09 21:31  江城qwe  阅读(23)  评论(0编辑  收藏  举报