Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8156559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:59:21+00:00 2026-06-06T16:59:21+00:00

I am new and I know I will get beat down for this Question

  • 0

I am new and I know I will get beat down for this Question but if I get the answer it will be worth it. I have a game that when the user wins the first round I want the save the score and restart the activity instead of going through all of the code and resetting every value. Now How do I keep the score from resetting when the Activity restarts. I dont have example as of yet.

 public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
// SET UP SPINS
private TextView spins;
private Button spin;

// SET UP CONTROLS
private TextView score;
private ProgressBar progressBar;
private static final int SHOW_BONUSACTIVITY = 1;
    int nextspin = 0;
int alert;
int total = 0;
int totalspins = 14;
public int gamescore = 0;
public int currentgamescore = 0;

   @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.play_layout);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Here is where I check if a new game for the first time or if there was a highscore saved to be used here

if (GameState.INSTANCE.getScore() != 0){
        gamescore = GameState.INSTANCE.getScore();}

IF USER WINS THEN THEY ARE SENT TO A BONUS GAME THEN BACK TO SEE IF THEY HAVE FINISHED THE GAME AND NEEDS TO START A NEW

   @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case (SHOW_BONUSACTIVITY): {
        if (resultCode == Activity.RESULT_OK) {
            int bonus = data.getIntExtra("money", 0);
            gamescore=(gamescore + bonus);
            score.setText(String.valueOf("SCORE: " + gamescore));
            if (bigwin == true){

HERE IS WHERE I SAVE THE GAMESCORE TO BRING IT INTO THE NEW ACTIVITY IF THEY WON THE FIRST LEVEL.

                GameState.INSTANCE.addScore(gamescore);
                Intent intent = new Intent(
                MainActivity.this,
                com.bigdaddyapp.android.blingo.MainActivity.class);
                startActivity(intent);  


            }
            }
        }
    }
    }

the next code is the enum activity

public enum GameState {
INSTANCE;

  private int score;
  private GameState(){
    score = 0;
  }

  public int getScore(){
    return score;
  }
  public void addScore(int score){
    this.score += score;
  }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:59:22+00:00Added an answer on June 6, 2026 at 4:59 pm

    As an alternative to storing the values persistent (using Shared Preferences or a SQLite Database), you can also store the values in memory but in a more global context. This way, you’ll keep the state over your re-creation of the Activity, but the application will forget them when it’s closed.

    This might not be what you need for your particular case, but sometimes it’s the better option.


    Using a Singleton, you can store the state of your application more globally and retrieve it at any point in your code. An example might be:

    public enum GameState{
      INSTANCE;
    
      private int score;
      private GameState(){
        score = 0;
      }
    
      public int getScore(){
        return score;
      }
      public void addScore(int score){
        this.score += score;
      }
    
    }
    

    It can be used by simply writing:

    GameState.INSTANCE.addScore(20);
    GameState.INSTANCE.getScore();
    

    There are multiple ways to implement a singleton. In my example, I used the approach Joshua Bloch documented in his Book “Effective Java – Second Edition”. See wikipedia for more information.


    Extending android.app.Application is another option, thought not as recommended as using the singleton. From the JavaDoc:

    Base class for those who need to maintain global application state.
    You can provide your own implementation by specifying its name in your
    AndroidManifest.xml’s tag, which will cause that class
    to be instantiated for you when the process for your
    application/package is created.

    A tutorial on how to do this might be found here (and the linked answer).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a noob question that I will get slated for but here
I have searched this question but didn't get / or understood the answer I
I am writing this little python program that will get new posts on the
As we know that ExtJS4 is new,we will get stuck with some problems even
Argh! I know I will get this eventually but at this point I'm almost
I know how to use new Date(UTCStrings) to local timezone. But now, the question
We know that malloc() and new operation allocate memory from heap dynamically, but where
I know that to instantiate a new OLEDbCommand object you do this: OleDbCommand command
(Sorry if it's a trivial question.) I have documents that looks like this (Python
As title says, i know that new throws an exception which can be caught,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.