You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio:扫描完成后跳转至新Activity的实现方法

扫描完成后跳转至新Activity的实现方案

方案1:弹窗确认后跳转

在你现有的弹窗确认按钮点击事件里,加入启动新Activity的逻辑即可,还能顺便把扫描结果传递给新页面使用:

修改后的代码:

ActivityResultLauncher<ScanOptions> barLauncher = registerForActivityResult(new ScanContract(), result ->{
        if (result.getContents()!=null){
            AlertDialog.Builder builder = new AlertDialog.Builder(QRScanActivity.this);
            builder.setTitle("Result");
            builder.setMessage(result.getContents());
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                    // 替换成你的目标Activity类,比如YourTargetActivity.class
                    Intent intent = new Intent(QRScanActivity.this, YourTargetActivity.class);
                    // 传递扫描结果到新页面,键名可自定义
                    intent.putExtra("SCAN_CONTENT", result.getContents());
                    startActivity(intent);
                    // 若不需要保留当前扫描页面,可添加此句
                    // finish();
                }
            }).show();
        }
    });

方案2:扫描完成直接跳转(跳过弹窗)

如果不需要展示结果弹窗,扫描结束后直接跳转页面,代码可以简化为:

ActivityResultLauncher<ScanOptions> barLauncher = registerForActivityResult(new ScanContract(), result ->{
        if (result.getContents()!=null){
            Intent intent = new Intent(QRScanActivity.this, YourTargetActivity.class);
            intent.putExtra("SCAN_CONTENT", result.getContents());
            startActivity(intent);
            finish();
        }
    });

关键注意点

  • 必须在AndroidManifest.xml中注册目标Activity,示例:
<activity android:name=".YourTargetActivity" />
  • 若新页面需要向当前页面返回数据,建议使用ActivityResultLauncher实现(替代旧版startActivityForResult),单纯跳转场景用startActivity即可。

内容的提问来源于stack exchange,提问作者Romeo Velloni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 07:10:46