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;
}
}