1

I am building a multi screen quiz app where each question is in a separate activity (I have 4 questions only). I have already set all the intents, so my app goes smoothly between screens (a user can click next/previous to go to the next/previous question). On the last screen which contains 4th question I added "Submit answers" button. A user clicks on a button and there should be a toast message displayed with the result of the quiz. I have 2 concerns though:

  1. Where do I put all the logic code? I mean the method that is called when the Submit Answers button is clicked and the if else statements where I calculate the result of each question. Can I put all the logic for example in the question 4 activity or should I split it between activities or should I create a separate class where I put only this logic? Please see below the logic that I am referring to:

    // This method is called when the Submit Answers button is clicked

    public void submitAnswers(View view) {
    
    //Getting the answer to question 1
    EditText answerQ1 = (EditText) findViewById(R.id.answer_robots);
    String answer = answerQ1.getText().toString();
    
    //Getting the answer to question 2 checkbox 1
    CheckBox checkBox1Q2 = (CheckBox) findViewById(R.id.checkbox1Q2);
    boolean isCheckBox1Q2 = checkBox1Q2.isChecked();
    
    //Getting the answer to question 2 checkbox 2
    CheckBox checkBox2Q2 = (CheckBox) findViewById(R.id.checkbox2Q2);
    boolean isCheckBox2Q2 = checkBox2Q2.isChecked();
    
    //Getting the answer to question 2 checkbox 3
    CheckBox checkBox3Q2 = (CheckBox) findViewById(R.id.checkbox3Q2);
    boolean isCheckBox3Q2 = checkBox3Q2.isChecked();
    
    //Getting the answer to question 3 checkbox 1
    CheckBox checkBox1Q3 = (CheckBox) findViewById(R.id.checkbox1Q3);
    boolean isCheckBox1Q3 = checkBox1Q3.isChecked();
    
    //Getting the answer to question 3 checkbox 2
    CheckBox checkBox2Q3 = (CheckBox) findViewById(R.id.checkbox2Q3);
    boolean isCheckBox2Q3 = checkBox2Q3.isChecked();
    
    //Getting the answer to question 3 checkbox 3
    CheckBox checkBox3Q3 = (CheckBox) findViewById(R.id.checkbox3Q3);
    boolean isCheckBox3Q3 = checkBox3Q3.isChecked();
    
    //Getting the answer to question 4 radio button 1
    RadioButton radioButton1Q4 = (RadioButton) findViewById(R.id.radiobutton1Q4);
    boolean isRadioButton1Q4 = radioButton1Q4.isChecked();
    
    //Calculate Question 1 result
    int resultQ1 = calculateResultQ1(answer);
    
    //Calculate Question 2 result
    int resultQ2 = calculateResultQ2(isCheckBox1Q2, isCheckBox2Q2, isCheckBox3Q2);
    
    //Calculate Question 3 result
    int resultQ3 = calculateResultQ3(isCheckBox1Q3, isCheckBox2Q3, isCheckBox3Q3);
    
    //Calculate Question 4 result
    int resultQ4 = calculateResultQ4(isRadioButton1Q4);
    
    //Calculate the quiz result
    int result = resultQ1 + resultQ2 + resultQ3 + resultQ4;
    
    //Display the quiz result in the Toast message
    Toast.makeText(this, "Congrats! Your score is " + result + ". Thank you for taking the quiz!", Toast.LENGTH_LONG).show();
    
    }
    
    /**
     * Check the answer to the open question 1
     *
     * @param userAnswer is the user's answer to the question 1
     * @return the score the user got for question 1
     */
    private int calculateResultQ1(String userAnswer) {
    int result = 0;
    String answer = "Robina";
    if (userAnswer.equals(answer)) {
        result = 1;
    }
    return result;
    }
    
    /**
     * Check which checkbox was selected in the question 2
     *
     * @param checkBox1 is whether or not the user checked the checkbox1
     * @param checkBox2 is whether or not the user checked the checkbox2
     * @param checkBox3 is whether or not the user checked the checkbox3
     * @return the score the user got for question 2
     */
    private int calculateResultQ2(boolean checkBox1, boolean checkBox2, boolean checkBox3) {
    int result = 0;
    if (checkBox1 && checkBox2 && checkBox3) {
        result = 1;
    }
    return result;
    }
    
    /**
     * Check which checkbox was selected in the question 3
     *
     * @param checkBox1 is whether or not the user checked the checkbox1
     * @param checkBox2 is whether or not the user checked the checkbox2
     * @param checkBox3 is whether or not the user checked the checkbox3
     * @return the score the user got for question 3
     */
    private int calculateResultQ3(boolean checkBox1, boolean checkBox2, boolean checkBox3) {
    int result = 0;
    if (checkBox1 && checkBox2) {
        result = 1;
    }
    
    if (checkBox3) {
        result = 0;
    }
    return result;
    }
    
    /**
     * Check which radio button was selected in the question 4
     *
     * @param radioButton1 is whether or not the user checked the radio       button 1
     * @return the score the user got for question 4
     */
    private int calculateResultQ4(boolean radioButton1) {
    int result = 0;
    if (radioButton1) {
        result = 1;
    }
    return result;
    }
    
    1. My second question is about how do I save the answers to each question so a user can switch between activities and the answers are not lost and also the results can be passed to calculate the final score?

I would be very grateful for your help as I am really stuck on this right now...

Thanks!

Nizam
  • 5,698
  • 9
  • 45
  • 57

3 Answers3

0

Justyna..I dont think that you need 4 activities for 4 questions. You should go with view pager. Please read the docs at https://developer.android.com/training/animation/screen-slide.html

Ashish Kumar
  • 374
  • 4
  • 11
0

1 Ans. You will do all the logic part inside separate class like helper class and call that method into your activity.

or

you can also do the logic part inside you activity

  1. Ans. if it is small amount of data you can use shared preference.

or

use parcelable in android for share data between activities.

vijay chhalotre
  • 396
  • 2
  • 11
0

Global Variable Or Application Context Variable -

you can store all your data for one session into Application Context, Globally. And create one non Activity class to calculate quiz result by passing all data from Application Context from Fourth Activity. and Later you can store your data in Sq-lite Database for future uses.

Below I provide one link to illustrate how you can implement Application Context for data storage for one session .

Global Variable Or Application Context Variable - Android Example

GlobalClass.java

Create your custom class subclass of android.app.Application class. you will use this class as global class for your Application environment(Conext).

package com.androidexample.globalvariable;

import android.app.Application;

public class GlobalClass extends Application{

    private String name;
    private String email;


    public String getName() {

        return name;
    }

    public void setName(String aName) {

       name = aName;

    }

    public String getEmail() {

        return email;
    }

    public void setEmail(String aEmail) {

      email = aEmail;
    }

}
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
  • Thank you for your answer. I need to check this as I am a beginner and this is entirely new concept for me. What do you think of using intent.putExta(), would this work here? It seems a bit simpler. – Justyna Goławska Feb 28 '17 at 09:53
  • Yes you can send data from one Activity to another through Intent and passing information in Bundle and then pass into Intent But make sure you should add all information of Activity One to Activity 2 then 3 end so on . And in fourth Activity you can manage your quiz details and calculate result. – Chetan Joshi Feb 28 '17 at 10:40