在开始下一个活动之前启动多个活动并获得结果



我正在制作一个测验应用程序,我的MainActivity(主菜单(使用startActivityForResult在QuestionActivity(问题文本和答案按钮(中启动QuestionActivity。用户回答问题后,我想将布尔值 a 发送回 MainActivity 以更新分数,然后可以将其推送到下一个意图中,在问题活动中,我在操作栏中显示分数。

问题是当我回答一个问题时,setResult 和 Finish 运行,但 onActivityResult 没有,在我回答所有问题后,OnActivityResult 运行 10 次。

如何在回答每个问题后运行 onActivityResult,而不是在最后运行?我需要使用意图标志吗?

额外信息

在主活动中,当用户开始测验时:

//Called when user clicks quiz
//Creates the list of questions and then asks them.
public void makeQuiz(View view) {
    //Pick the questions for the quiz
    question[] quiz = new question[10]; //A quiz with 10 questions
    for (int i = 0; i < quiz.length; i++) {
        quiz[i] = myDBHelper.pickQuestion();
        askQuestion(view, quiz[i],i,qscore);
        Log.d("Asked question", Integer.toString(i));
    }
}
提问

用于启动提问活动:

    //Creates a question and then passes it though to the question view.
public boolean askQuestion(View view, question q, int questionNum, int qscore){
    question q1 = q;
    Log.d("Correct Ans",q.CorrectAns);
    Intent question = new Intent(this, QuestionActivity.class);
    Bundle extras = new Bundle();
    extras.putString("QUESTION", q.QuestionText);
    extras.putString("MODULE", q.Module);
    extras.putString("CORRECT_ANS",q.CorrectAns);
    extras.putString("ANS1", q.WAns[0]);
    extras.putString("ANS2", q.WAns[1]);
    extras.putString("ANS3", q.WAns[2]);
    extras.putInt("qscore",qscore);
    question.putExtras(extras); //Passing the question to the QuestionActivity
    startActivityForResult(question,1);
    return true;
}

在"问题活动"中,当用户正确回答问题时:

//Pass back that we got the correct answer
resultIntent = new Intent();
resultIntent.putExtra("ANSWER",true);
setResult(1, resultIntent);
Log.d("True", "Set result has been called");
finish();

回到主活动:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode,resultCode,data);
    //Check which event we are responding to
        Log.d("onActivityResult", "called"); //This never runs
        if(resultCode == 1){
            //Do something with the intent
            //if q is correct, update the score in shared prefrences,
            Boolean result = data.getBooleanExtra("ANSWER",false);
            Log.d("ANSWER IS ", Boolean.toString(result));
            qscore += result ? 1:0; //This updated score is then pushed into the next intent so it can be displayed in the next question activity. 
        }
}

好的,所以这是你的问题,你开始结果的活动:

startActivityForResult(question,questionNum);

所以问题数字是你的请求代码

但是当你完成问题活动时,你像这样完成它:

setResult(Activity.RESULT_OK, resultIntent);

所以这里的请求代码是Activity.RESULT_OK的值

你需要他们平等。

编辑:

对于您在评论中的请求,请查看以下内容:

private static final int REQUEST_CODE = 123131;
private Stack<Intent> intentStack = new Stack<>();
//Called when user clicks quiz
//Creates the list of questions and then asks them.
    public void makeQuiz(View view) {
        //Pick the questions for the quiz
        question[] quiz = new question[10]; //A quiz with 10 questions
        for (int i = 0; i < quiz.length; i++) {
            quiz[i] = myDBHelper.pickQuestion();
            askQuestion(view, quiz[i], i, qscore);
            Log.d("Asked question", Integer.toString(i));
        }
        startActivityForResult(intentStack.pop(), REQUEST_CODE);
    }
//Creates a question and then passes it though to the question view.
public boolean askQuestion(View view, question q, int questionNum, int qscore) {
    question q1 = q;
    Log.d("Correct Ans", q.CorrectAns);
    Intent question = new Intent(this, QuestionActivity.class);
    Bundle extras = new Bundle();
    extras.putString("QUESTION", q.QuestionText);
    extras.putString("MODULE", q.Module);
    extras.putString("CORRECT_ANS", q.CorrectAns);
    extras.putString("ANS1", q.WAns[0]);
    extras.putString("ANS2", q.WAns[1]);
    extras.putString("ANS3", q.WAns[2]);
    extras.putInt("qscore", qscore);
    question.putExtras(extras); //Passing the question to the QuestionActivity
    intentStack.push(question);
    return true;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //Check which event we are responding to
    if (resultCode == RESULT_OK) {
        //Do something with the intent
        //if q is correct, update the score in shared prefrences,
        Boolean result = data.getBooleanExtra("ANSWER", false);
        Log.d("ANSWER IS ", Boolean.toString(result));
        qscore += result ? 1 : 0;
        if(!intentStack.isEmpty()){
            startActivityForResult(intentStack.pop(), REQUEST_CODE);
        }
    }
}

最新更新