随笔 - 7  文章 - 0  评论 - 0  阅读 - 521

安卓开发学习-向上一个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   江城qwe  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示