0

In my MainActivity i have a score variable, int score = 0;

however i have another activity which is the DisplayScore activity (which displays the score) txtFinalScore.setText("Score: " + score);

how can i use that same variable form my MainActivity to my DisplayScore activity so that i can display the score?

2 Answers2

0

If I've understood well, here's how you should do it.

public class Main {
    public static void main(String[] args) {
        int score = 0; // the main activity thing

        int displayScore = score; // the thing to use the same value as score

        System.out.println("Score : " + displayScore); // to simulate the setText
   }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
0rb
  • 19
  • 5
  • This code has no activities in it. OP is referring to the concept of `Activity` in an Android app, not just to simple local variables. – Silvio Mayolo Apr 19 '22 at 01:23
  • I see, then how do we do it ? – 0rb Apr 19 '22 at 01:26
  • Sorry, I'd post a counter-answer if I could. But I've done very little mobile dev, and it was awhile ago, so I'll let someone with more recent Android experience answer this. – Silvio Mayolo Apr 19 '22 at 01:28
  • Alright! It's all good. – 0rb Apr 19 '22 at 01:29
  • Appreciate the help! But i asked something different, which has been solved now anyways, i wanted to use a variable from one Activity to another i Android Studio, i did this by using `getExra()` and `PutExtra()` methods. – DEV-rakibul Apr 19 '22 at 01:57
  • Oh, sorry if it didn't help but i'm glad you got your answer! – 0rb Apr 19 '22 at 18:32
0

This question explains perfectly how to use putExtra() and getExtra() methods for the Intent in Android, because I think that's what you need.

Also, if you find this difficult, you can store the score value into a sharedPreference and retrieve it from another activity. You can see the docs here.

AhmedSHA256
  • 525
  • 2
  • 20