1

I'm a noobie making a quiz in Android Studio and i'm trying to pass an integer between activities to add to the amount of questions they got correct for the end but in the second activity it isn't changing when I answer the first question correct.

Question1 activity:

    public class Question1 extends AppCompatActivity {

        public int correctAnswers = 0;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_question1);

            Intent intent = new Intent(Question1.this, Question2.class);
            Intent i = getIntent();
            Intent answersCorrect = new Intent(Question1.this, Question2.class);
            answersCorrect.putExtra("correctAnswers", correctAnswers);
        }
                    public void submitQuestion1(View view) {
            EditText question1TextInput = (EditText) findViewById(R.id.question1TextInput);
            if (question1TextInput.getText().toString().length() >= 1) {
                startActivity(new Intent(Question1.this, Question2.class));
                if (question1TextInput.getText().toString().toUpperCase().contentEquals("FATHER")) {
                    correctAnswers += 1;
                    Intent answersCorrect = new Intent(Question1.this, Question2.class);
                    answersCorrect.putExtra("correctAnswers", correctAnswers);
                }
            }
        }
    }

Question2 Activity:

public class Question2 extends AppCompatActivity {

    public int correctAnswers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question2);
        Intent intent = getIntent();
        int number = intent.getIntExtra("correctAnswers", 0);
        TextView myAwesomeTextView = (TextView)findViewById(R.id.text);

        myAwesomeTextView.setText(String.valueOf(number));

    }
}
letsintegreat
  • 3,328
  • 4
  • 18
  • 39

3 Answers3

0

Move the intent to a field. You only need it once.

Then, the issue is that you start the other activity too soon, without setting any value. You started the other activity with an empty, new Intent

public class Question1 extends AppCompatActivity {

    public int correctAnswers = 0;
    final Intent answersIntent = new Intent(Question1.this, Question2.class);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question1);
    }

    public void submitQuestion1(View view) {
        EditText question1TextInput = (EditText) findViewById(R.id.question1TextInput);
        String answer = question1TextInput.getText().toString();

        // No need to check for length if directly checking another string
        if (answer.toUpperCase().contentEquals("FATHER")) {
            answersIntent.putExtra("correctAnswers", ++correctAnswers);
            startActivity(answersIntent);
        }

    }

If you plan on sharing that value over many questions, try SharedPreferences.

FWIW, make a generic View for any question that has the question text and possible answer fields. Try not to make one activity per question.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

you duplicate your intent here:

Intent intent = new Intent(Question1.this, Question2.class);
Intent i = getIntent();
Intent answersCorrect = new Intent(Question1.this, Question2.class);
answersCorrect.putExtra("correctAnswers", correctAnswers);

replace it to:

Intent intent = new Intent(Question1.this, Question2.class);
intent.putExtra("correctAnswers", correctAnswers);
startActivity(intent);

in your second Activity:

int correctAnswers;

correctAnswers = (int) getIntent().getIntExtra("correctAnswers", 0);
Yossi
  • 327
  • 2
  • 5
0

So basically, when you have one activity and want to open a second activity, an Intent is the most important thing to have. It's responsible for communcation between your system and your application.

Intent is responsible for starting an activity, starting a service, and delivering a broadcast.

Note that there are two different types of intent: Explicit and Implicit.

Explicit Intent is used in this manner: You have Activity_1 and you KNOW that you want to start Acticity_2 FROM Activity_1.

Implicit Intent is used when you DON'T know the name of the activity that you want to start.

Now, I know you probably understand what the StartActivity() method DOES, but StartActivity always requires an intent to go into the parenthesis. StartActivity(Activity_2); will not work.

So, when using Explicit Intent:

Intent i = new Intent(Activity_1.this, Activity_2.class);
StartActivity(i);

You start with making a reference - i - and, inside the parameters, the first being the activity from which you are calling the second activity, and the second being the activity which you want to call.

Here's a video on Intents as well: https://youtu.be/FH1Ym1KjJNc

Hope this helped.

Ben
  • 13
  • 7
  • This explains Intents, doesn't really solve the problem in the question, though – OneCricketeer Apr 12 '18 at 03:18
  • @cricket_007 Yeah, I really responded to his top question first because us giving him the direct answer probably wouldn't help him become better at Android Studio. It's the difference (in my opinion) between understanding and copy/pasting. – Ben Apr 12 '18 at 03:34
  • 1
    Sure. I'm just saying that the misunderstanding is likely happening because of the problem in the code, which isn't really solved by this answer. Feel free to edit to include additional information – OneCricketeer Apr 12 '18 at 13:05