In have a problem either with reading the resultcode of an Activity
or writing the resultcode in the SharedPreferences
.
The app is a quiz which should show the player's highscore.
Frist my Start Menu Activity
:
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_menu);
init();
}
private void init() {
// init Ui Elements
Button startTimeQuizBT = (Button) findViewById(R.id.StartTimeQuiz);
// init onClickListeners for Buttons
startTimeQuizBT.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick( View v ) {
Intent intent = new Intent(getApplicationContext(), QuizTimeActivity.class);
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onResume() {
super.onResume();
TextView ScoreTV = (TextView) findViewById(R.id.highscoreTV);
ScoreTV.setText("Aktueller Highscore : " + Integer.toString(readHighscore()));
}
private int readHighscore() {
SharedPreferences pref = getSharedPreferences("GAME", 0);
return pref.getInt("HIGHSCORE", 0);
}
@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data ) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode ==1) {
if (resultCode > readHighscore()) {
writeHighscore(resultCode);
}
}
}
private void writeHighscore( int highscore ) {
SharedPreferences pref = getSharedPreferences("GAME", 0);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("HIGHSCORE", highscore);
editor.apply();
}
}
This Activity
is basically the main menu and should start QuizTimeActivity
for Result
and showing the player's current highscore.
Secondly the QuizTimeActivity
:
QuestionLib qLib = new QuestionLib();
String TAG = "QuizTimeActivity";
private Button choice1;
private Button choice2;
private Button choice3;
private Button choice4;
private TextView questionTV;
private TextView scoreTV;
private int score = 0;
private int randomNumber = 0;
private String correctAnswer;
private TextView countdownTV;
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_time);
countdownTV = (TextView) findViewById(R.id.countdown);
CountDownTimer timer = new CountDownTimer(90 * 1000, 100) {
@Override
public void onTick( long millisUntilFinished ) {
countdownTV.setText(String.valueOf(millisUntilFinished / 1000));
}
@Override
public void onFinish() {
setResult(score);
Log.i(TAG, "current score : " + score);
startActivity(new Intent(getApplicationContext(), StartMenuActivity.class));
finish();
}
}.start();
choice1 = (Button) findViewById(R.id.choice1);
choice2 = (Button) findViewById(R.id.choice2);
choice3 = (Button) findViewById(R.id.choice3);
choice4 = (Button) findViewById(R.id.choice4);
questionTV = (TextView) findViewById(R.id.question);
scoreTV = (TextView) findViewById(R.id.scoreTV);
generateRandomNumber(0, 162);
updateQuestion();
choice1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick( View view ) {
if (choice1.getText() == correctAnswer) {
blinkEffectGreen(choice1);
score++;
}
else {
blinkEffectRed(choice1);
}
}
});
choice2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick( View view ) {
if (choice2.getText() == correctAnswer) {
blinkEffectGreen(choice2);
score++;
}
else {
blinkEffectGreen(choice2);
}
}
});
choice3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick( View view ) {
if (choice3.getText() == correctAnswer) {
blinkEffectGreen(choice3);
score++;
}
else {
blinkEffectRed(choice3);
}
}
});
choice4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick( View view ) {
if (choice4.getText() == correctAnswer) {
blinkEffectGreen(choice4);
score++;
}
else {
blinkEffectRed(choice4);
}
}
});
Somehow the highscore stays at zero when QuiztimeActivty
ends after 90 seconds.
Am I doing something wrong here(I guess yes, otherwise it would bn working...)?
I hope these infos are enough, if not I will provide more.
Thank you!
First you need to change the code in the onFinish()
method:
@Override
public void onFinish() {
Intent intent = new Intent();
intent.putExtra("MY_SCORE", score);
setResult(RESULT_OK, intent);
finish();
}
Then you need to change the code in the onActivityResult()
method:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
int score = data.getIntExtra("MY_SCORE", -1);
if (score > readHighscore()) {
writeHighscore(resultCode);
}
}
}
}
You should not use startActivity()
in your onFinish()
method because that will start a new instance of the Activity. Just let the Activity finish and the focus will return to the Activity that started it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.