-3

I am working on app that will display the score in a textview and will then get that score in another activity. Am doing this using the following code:

        //in the first activity
        int score= 0;

        score= score + 5;
        txtscore.setText("Score: " + score);


        Intent intent = new Intent(context, AnotherActivity.class);
        intent.putExtra("Score",score+ " ");

        context.startActivity(intent);

         //in second activity (AnotherActivity.class)
          Bundle bundle = getIntent().getExtras();

        if (bundle != null){

        int score= bundle.getInt("0");
        txtscore.setText("0" + score);
    }

Now I need to use the score that has been save in txtscore in a static method in the second class itself, whereby 5 points need to be added making a total of 10.

I have a static method:

    private static void rotate{
         //some tasks being done

        if (isSolved()){

        int new =0;

        // need to get value from another activity here and save to x

        new = x + 10;
        txtscore.setText("Score: " + new );

    }

}

Any help?

Panda18
  • 57
  • 6

2 Answers2

0

Just use:

getIntent().getExtras().getString("Score");

in your second Activity

julien-100000
  • 871
  • 8
  • 23
0

Rather than passing the score extra as a String, which would then have to be parsed to an int, it is better to bundle the score as an int:

Activity One

int score = 5;
Intent.putExtra("score", score);

Activity Two

int score = bundle.getIntExtra("score");
score += 5;
txtScore.setText("SCORE: " + score);
Bryon Nicoson
  • 676
  • 10
  • 10