我正在制作一个测验应用程序,我的MainActivity(主菜单)使用startActivityForResult在QuestionActivity(问题文本和答案按钮)中启动QuestionActivity。在用户回答完问题之后,我想将一个布尔值发送回MainActivity以更新得分,然后可以将其推入下一个意图,在问题活动中,我在操作栏中显示得分。
问题是,当我回答一个问题,setResult和Finish Runs但是onActivityResult没有,在我回答所有问题之后,OnActivityResult会运行10次。
在回答每个问题后,我怎样才能让onActivityResult运行,而不是在最后? 我需要使用意图标志吗?
在MainActivity中,当用户开始测验时:
//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));
}
}
Ask Question用于启动QuestionActivity:
//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;
}
在QuestionActivity中,当用户正确回答问题时:
//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();
返回MainActivity:
@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.
}
}
答案 0 :(得分:0)
好的,所以这是你的问题,你可以通过以下方式开始结果活动:
startActivityForResult(question,questionNum);
所以questionNum是你的requestCode
但是当你完成QuestionActivity时,你会这样完成:
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);
}
}
}