Reputation: 33
I am trying to compare 2 scores that I have made oldScore and best_score(both in Main_Screen). I think the problem is that the ints are not saving properly. There are no errors but if the best_Score is lower than the old_score it still changes it in the textview even though the best_score should be higher than the old_score. Hopefully I can learn from this :)
Main_Screen
public class Main_Screen extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
getSupportActionBar().hide();
//SETS CUSTOM FONT
TextView main_screen_titleone = (TextView)findViewById(R.id.main_screen_titleone);
TextView main_screen_titletwo = (TextView)findViewById(R.id.main_screen_titletwo);
TextView best_score_tv = (TextView)findViewById(R.id.best_score_tv);
TextView best_score_number_tv = (TextView)findViewById(R.id.best_score_number_tv);
Typeface myCustomFont = Typeface.createFromAsset(getAssets(), "fonts/LemonMilk.otf");
main_screen_titleone.setTypeface(myCustomFont);
main_screen_titletwo.setTypeface(myCustomFont);
best_score_tv.setTypeface(myCustomFont);
best_score_number_tv.setTypeface(myCustomFont);
//******************;
int best_score = retrieveInt("BEST_SCORE");
int oldScore = Integer.valueOf(best_score_number_tv.getText().toString());
if (best_score > oldScore){
best_score_number_tv.setText(best_score + "");
}
}
public void startTheGame(View view){
Intent intent = new Intent(this, press_screen.class);
startActivity(intent);
}
public int retrieveInt(String key){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
return sp.getInt(key, 0);
}
Press_Screen
public class press_screen extends ActionBarActivity {
private int time_left;
private int amountOfTapsNumber;
private int bestScore;
TextView amountOfTaps;
TextView timeLeftNumber;
TextView time_left_tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_press_screen);
getSupportActionBar().hide();
amountOfTaps = (TextView)findViewById(R.id.amount_of_taps);
timeLeftNumber = (TextView)findViewById(R.id.time_left_number_tv);
time_left_tv = (TextView)findViewById(R.id.time_left_tv);
Typeface myCustomFont = Typeface.createFromAsset(getAssets(), "fonts/LemonMilk.otf");
amountOfTaps.setTypeface(myCustomFont);
timeLeftNumber.setTypeface(myCustomFont);
time_left_tv.setTypeface(myCustomFont);
timer.start();
}
//Create Timer
CountDownTimer timer = new CountDownTimer(21000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timeLeftNumber = (TextView) findViewById(R.id.time_left_number_tv);
time_left = Integer.valueOf(timeLeftNumber.getText().toString()) - 1;
timeLeftNumber.setText(time_left + "");
}
@Override
public void onFinish() {
if(amountOfTapsNumber > bestScore){
bestScore = amountOfTapsNumber;
saveInfo("BEST_SCORE", bestScore);
}
TextView best_score_number_tv = (TextView)findViewById(R.id.best_score_number_tv);
Intent goBackToMainActivity = new Intent(press_screen.this, Main_Screen.class);
startActivity(goBackToMainActivity);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_press_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void triggerTapOnMainButton(View view) {
amountOfTaps = (TextView) findViewById(R.id.amount_of_taps);
amountOfTapsNumber = Integer.valueOf(amountOfTaps.getText().toString()) + 1;
amountOfTaps.setText(amountOfTapsNumber + "");
}
public void saveInfo(String key, int bestScore){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor edit = sp.edit();
edit.putInt("BEST_SCORE", bestScore);
edit.commit();
}
}
Upvotes: 1
Views: 38
Reputation: 6892
When your timer finishes, you send your tapsNumber on the Intent to MainActivity, like this:
Intent goBackToMainActivity = new Intent(press_screen.this, Main_Screen.class);
Bundle bundle = new Bundle();
bundle.putInt("lastScore", amountOfTapsNumber);
goBackToMainActivity.putExtras(bundle);
startActivity(goBackToMainActivity);
Then, in your MainActivity onCreate()
, check if your Intent has the oldScore
. If it does, compare it to the best_score
.
int lastScore = 0;
if(getIntent() !=null && getIntent().getIntExtra("lastScore",0)>0)
lastScore = getIntent().getIntExtra("lastScore",0);
//comparing the lastScore with the bestScore
if (lastScore > best_Score){
best_score_number_tv.setText(lastScore + "");
}
else{
best_score_number_tv.setText(best_Score + "");
}
This way your bestScore will be updated if your lastScore is higher than your old best_score.
Upvotes: 1