Herbert Willkomm
Herbert Willkomm

Reputation: 41

how to pass Integer to another Activity on Android Studio

I am developing simple game on android studio, where you need to identify the given image. each image has 40 points but every time you answer it wrong, your 40 points will be deducted by 10 points. The points will be accumulated until you finish identify the 10 picture. My problem is, I don't know how to pass the points to another activity so that I can add it. here's my Java code.. PS. sorry I am so new in android studio and also in StackOverflow, I don't know how to compose a question.

    public void onClickSubmit (View view) {

        answer = (EditText) findViewById(R.id.etAnswer);
        tvResult = (TextView) findViewById(R.id.resultLabel);

        if (answer.getText().toString().equals("apple")) {
            Intent intent = new Intent(this, appleTrivia.class);
            startActivity(intent);
            tvResult.setText("Good eye sight");
            tvResult.setTextColor(0xFF00FF00);
            timer.cancel();
            totalScorePoints = scorePoints;

            String result = String.valueOf(totalScorePoints);
            pointsLabel.setText("Score: " + result);
            answer.setText("");



        } else {
            tvResult.setText("No no no. try again.");
            tvResult.setTextColor(0xFFFF0011);
            answer.setText("");
            scorePoints = scorePoints - minusPoint;
            totalScorePoints = scorePoints;
            String result = String.valueOf(totalScorePoints);
            pointsLabel.setText("Score: " + result);

How can I pass the totalScorePoint to my second activity so that the points from first activity will be added to the points of second activity. tnx to all who will response to my noob problem ^_^ This is my second activity.`enter code here public void onClickSubmit (View view) {

    answer = (EditText) findViewById(R.id.etAnswer);
    tvResult = (TextView) findViewById(R.id.resultLabel);

    if (answer.getText().toString().equals("cherry")) {
        Intent intent = new Intent(this, cherryTrivia.class);
        startActivity(intent);
        tvResult.setText("Good eye sight");
        tvResult.setTextColor(0xFF00FF00);
        timer.cancel();

        totalScorePoints = scorePoints;

        String result = String.valueOf(totalScorePoints);
        pointsLabel.setText("Score: " + result);
        answer.setText("");
    } else {
        tvResult.setText("No no no. try again.");
        tvResult.setTextColor(0xFFFF0011);
        answer.setText("");
        scorePoints = scorePoints - minusPoint;
        totalScorePoints = scorePoints;
        String result = String.valueOf(totalScorePoints);
        pointsLabel.setText("Score: " + result);



    }
}
}


 <!-- end snippet -->

Upvotes: 1

Views: 949

Answers (1)

cwbowron
cwbowron

Reputation: 1025

On the sending side:

Intent intent = new Intent(this, cherryTrivia.class);
intent.putExtra( "points", scorePoints );
startActivity(intent);

On the receiving side:

getIntent().getIntExtra( "points", 0 );

Upvotes: 3

Related Questions