安卓APP页面之间传参(Android studio 开发)

页面之间传参

  • 关键核心功能
  • 主界面(监听参数)
  • [SeedCalibrationActivity 副界面(发送参数)](#SeedCalibrationActivity 副界面(发送参数))
  • [FertCalibrationActivity 副界面(发送参数)](#FertCalibrationActivity 副界面(发送参数))
  • [AndroidManifest.xml (三个界面)](#AndroidManifest.xml (三个界面))

关键核心功能

java 复制代码
private ActivityResultLauncher<Intent> calibrationLauncher;
  1. 结果处理复用
  • 单个 ActivityResultLauncher 处理两种不同类型的校准结果
  • 通过检查返回数据中的特定字段区分种子/肥料结果
  1. 数据驱动更新
  • 依赖返回 Intent 中的 extras 传递校准结果
  • 使用一致的键名约定:seed_name, seed_calibration_value等
  1. 健壮性处理
  • 结果码检查 (RESULT_OK)
  • 空数据检查 (result.getData() != null)
  • 空类型检查 (seedType != null)
  • 默认值回退 (0.7178)
  1. 模块化设计​​
  • 分离结果处理与活动启动逻辑
  • 统一的启动器处理不同活动

代码思路图

主界面(监听参数)

java 复制代码
public class MainActivity extends AppCompatActivity
java 复制代码
// 注册 ActivityResultLauncher
calibrationLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        result -> {
            if (result.getResultCode() == RESULT_OK && result.getData() != null) {
                String seedType = result.getData().getStringExtra("seed_name");
                Double seedcalValue = result.getData().getDoubleExtra("seed_calibration_value", 0.7178);
                String fertType = result.getData().getStringExtra("fert_name");
                Double fertcalValue = result.getData().getDoubleExtra("fert_calibration_value", 0.7178);

                if (seedType != null) {
                    update_seedtype_calvalue(seedType, seedcalValue);
                }

                if (fertType != null) {
                    update_ferttype_calvalue(fertType, fertcalValue);
                }
            }
        }
);

// 启动 SeedCalibrationActivity 的按钮
Button btnSeedCalibrate = findViewById(R.id.btn_seed_calibration);
btnSeedCalibrate.setOnClickListener(v -> {
    Intent intent = new Intent(MainActivity.this, SeedCalibrationActivity.class);
    calibrationLauncher.launch(intent);
});

// 启动 SeedCalibrationActivity 的按钮
Button btnFertCalibrate = findViewById(R.id.btn_fert_calibration);
btnFertCalibrate.setOnClickListener(v -> {
    Intent intent = new Intent(MainActivity.this, FertCalibrationActivity.class);
    calibrationLauncher.launch(intent);
        });

SeedCalibrationActivity 副界面(发送参数)

java 复制代码
public class SeedCalibrationActivity extends AppCompatActivity
java 复制代码
private void saveCalibrationResult() {
    // 创建返回结果
    Intent resultIntent = new Intent();
    resultIntent.putExtra("seed_name", currentSeedName);
    resultIntent.putExtra("seed_calibration_value", currentCalibrationValue);
    setResult(RESULT_OK, resultIntent);

    // 显示保存成功提示
    Toast.makeText(this, currentSeedName + " 校准值已保存", Toast.LENGTH_SHORT).show();
    finish();
}

FertCalibrationActivity 副界面(发送参数)

java 复制代码
public class FertCalibrationActivity extends AppCompatActivity
java 复制代码
private void saveCalibrationResult() {
    // 创建返回结果
    Intent resultIntent = new Intent();
    resultIntent.putExtra("fert_name", currentFertName);
    resultIntent.putExtra("fert_calibration_value", currentCalibrationValue);
    setResult(RESULT_OK, resultIntent);

    // 显示保存成功提示
    Toast.makeText(this, currentFertName + " 校准值已保存", Toast.LENGTH_SHORT).show();
    finish();
}

AndroidManifest.xml (三个界面)

xml 复制代码
<activity
    android:name=".MainActivity"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<!-- 播种校准Activity -->
<activity
    android:name=".SeedCalibrationActivity"
    android:label="播种校准"
    android:parentActivityName=".MainActivity"
    android:theme="@style/AppTheme.NoActionBar" />

<!-- 施肥校准Activity -->
<activity
    android:name=".FertCalibrationActivity"
    android:label="施肥校准"
    android:parentActivityName=".MainActivity"
    android:theme="@style/AppTheme.NoActionBar" />
相关推荐
代码不停1 小时前
Spring Boot快速入手
java·spring boot·后端
-Excalibur-1 小时前
关于计算机网络当中的各种计时器
java·c语言·网络·c++·笔记·python·计算机网络
小宇的天下1 小时前
Calibre nmDRC 运行机制与规则文件(13-1)
java·开发语言·数据库
阿拉斯攀登1 小时前
设计模式:实战概要
java·设计模式
阿拉斯攀登1 小时前
设计模式:工厂模式概要
java·设计模式·抽象工厂模式
曹轲恒1 小时前
Java Collections & Arrays 工具类
java
.卡1 小时前
(022)FastJson 序列化导致的 java.util.ConcurrentModificationException
java
武子康1 小时前
Java-218 RocketMQ Java API 实战:同步/异步 Producer 与 Pull/Push Consumer
java·大数据·分布式·消息队列·rocketmq·java-rocketmq·mq
不爱吃米饭_1 小时前
Spring Security、Apache Shiro、Sa-Token,主流安全框架如何选择?
java·安全
我命由我123451 小时前
Java 开发 - 含有 null 值字段的对象排序(自定义 Comparator、使用 Comparator、使用 Stream API)
java·开发语言·学习·java-ee·intellij-idea·学习方法·intellij idea