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

Android如何在多个Activity间传递整数并在指定Activity汇总?

多Activity整数传递并汇总的正确实现方法

首先修正你当前代码的核心问题:
你在QuizSecond的submitBtn点击事件里创建了带score的Intent,但没有调用startActivity(quizIntent)启动页面;反而在loadNewQuestion方法中跳转ComputeActivity时,使用的是不带任何Extra的Intent,这直接导致ComputeActivity无法获取到score值。

针对四个Activity收集整数并在ComputeActivity汇总的需求,推荐以下几种实用方案:

方案一:链式Intent传递(逐个传递累加)

每个Activity启动下一个页面时,将之前所有收集到的整数和当前Activity的整数一起通过Intent传递,最终在ComputeActivity取出所有数值汇总。

实现步骤:

  1. 第一个Activity(如QuizFirst)传递数值到第二个Activity
    在QuizFirst完成答题后,启动QuizSecond时传入自身的得分:

    Intent intent = new Intent(QuizFirst.this, QuizSecond.class);
    intent.putExtra("EXTRA_SCORE_FIRST", score1); // score1是QuizFirst的得分
    startActivity(intent);
    
  2. 第二个Activity(QuizSecond)接收并传递数值
    在QuizSecond的onCreate中接收第一个Activity的数值:

    int scoreFromFirst = getIntent().getIntExtra("EXTRA_SCORE_FIRST", 0);
    

    当QuizSecond完成所有题目后,启动下一个Activity(或直接跳转到ComputeActivity)时,传递两个数值:

    void loadNewQuestion(){
        if(currentQuestionIndex2 == totalQuestion2 ){
            Intent intent = new Intent(QuizSecond.this, QuizThird.class);
            // 传递第一个Activity的得分
            intent.putExtra("EXTRA_SCORE_FIRST", scoreFromFirst);
            // 传递当前Activity的得分
            intent.putExtra("EXTRA_SCORE_SECOND", score);
            startActivity(intent);
            finish(); // 结束当前页面,避免返回重复统计
            return;
        }
        // 原有加载题目代码不变
    }
    
  3. ComputeActivity汇总所有数值
    在ComputeActivity中取出四个Activity的数值并计算总和:

    int score1 = getIntent().getIntExtra("EXTRA_SCORE_FIRST", 0);
    int score2 = getIntent().getIntExtra("EXTRA_SCORE_SECOND", 0);
    int score3 = getIntent().getIntExtra("EXTRA_SCORE_THIRD", 0);
    int score4 = getIntent().getIntExtra("EXTRA_SCORE_FOURTH", 0);
    int totalScore = score1 + score2 + score3 + score4;
    
    // 后续逻辑,比如Toast提示
    if(totalScore > 12){ // 根据实际总题数调整阈值
        Toast.makeText(ComputeActivity.this, "总分达标", Toast.LENGTH_LONG).show();
    }
    

方案二:自定义Application存储全局变量

通过自定义Application类保存所有得分,每个Activity完成后更新对应变量,最终在ComputeActivity直接读取汇总。

实现步骤:

  1. 创建自定义Application类

    public class QuizApp extends Application {
        private int score1, score2, score3, score4;
    
        public int getScore1() { return score1; }
        public void setScore1(int score1) { this.score1 = score1; }
        public int getScore2() { return score2; }
        public void setScore2(int score2) { this.score2 = score2; }
        public int getScore3() { return score3; }
        public void setScore3(int score3) { this.score3 = score3; }
        public int getScore4() { return score4; }
        public void setScore4(int score4) { this.score4 = score4; }
    }
    
  2. 在Manifest中注册Application
    修改AndroidManifest.xml的<application>标签,指定自定义Application:

    <application
        android:name=".QuizApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.QuizApp">
        <!-- 其他Activity注册代码 -->
    </application>
    
  3. 每个Activity更新得分
    比如QuizFirst完成后:

    QuizApp app = (QuizApp) getApplicationContext();
    app.setScore1(score1);
    startActivity(new Intent(QuizFirst.this, QuizSecond.class));
    

    QuizSecond完成后:

    QuizApp app = (QuizApp) getApplicationContext();
    app.setScore2(score);
    startActivity(new Intent(QuizSecond.this, QuizThird.class));
    
  4. ComputeActivity汇总得分

    QuizApp app = (QuizApp) getApplicationContext();
    int totalScore = app.getScore1() + app.getScore2() + app.getScore3() + app.getScore4();
    // 后续逻辑处理
    

方案三:使用SharedPreferences持久化存储

将每个Activity的得分存入SharedPreferences,ComputeActivity读取所有得分并汇总,适合需要持久化保存得分的场景。

实现步骤:

  1. 每个Activity存储得分
    在Activity完成答题后,将得分存入SharedPreferences:

    SharedPreferences sp = getSharedPreferences("QuizScores", MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    editor.putInt("score2", score); // 第二个Activity的得分
    editor.apply();
    
  2. ComputeActivity读取并汇总

    SharedPreferences sp = getSharedPreferences("QuizScores", MODE_PRIVATE);
    int score1 = sp.getInt("score1", 0);
    int score2 = sp.getInt("score2", 0);
    int score3 = sp.getInt("score3", 0);
    int score4 = sp.getInt("score4", 0);
    int totalScore = score1 + score2 + score3 + score4;
    // 后续逻辑处理
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 07:01:04