I am making a small game in Android, but I don't know hot to save the high score by using SharedPreferences. When I Intent the point in GamePlayActivity, I want to replace the older highscore by the newer highscore if it bigger than the older.
Here is my code:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class GameOverActivity extends Activity{
TextView tvHighScore, tvScore;
Button btnRetry;
int score = 0;
int highScore = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_gameoveractivity);
tvHighScore = (TextView) findViewById(R.id.tvHighScore);
tvScore = (TextView) findViewById(R.id.tvScore);
btnRetry = (Button) findViewById(R.id.btnRetry);
Intent getIntent = getIntent();
score = getIntent.getIntExtra("point", 0);
tvScore.setText(Integer.toString(score));
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
highScore = prefs.getInt("score", 0);
if(score > highScore){
highScore = score;
tvHighScore.setText(Integer.toString(highScore));
}
btnRetry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(GameOverActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onDestroy() {
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("score", highScore);
editor.commit();
super.onDestroy();
}
}
And XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/uefa_champion_league_background">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GUESS THE \n FOOTBALL PLAYER"
android:textSize="30dp"
android:typeface="serif"
android:gravity="center"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:textColor="@color/red"
android:textStyle="bold"
android:shadowColor="@color/green2"
android:shadowDx="5"
android:shadowDy="5"
android:shadowRadius="15"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="186dp"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@color/blue25"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GAME OVER"
android:textSize="40dp"
android:typeface="serif"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<LinearLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TOP SCORE"
android:typeface="serif"
android:textSize="15dp"/>
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:text="1"
android:textSize="30dp"
android:typeface="serif"
android:gravity="center"
android:textColor="@color/red2"
android:textStyle="bold"
android:id="@+id/tvHighScore"/>
</LinearLayout>
<Button
android:layout_width="120dp"
android:layout_height="80dp"
android:text="RETRY"
android:textSize="30dp"
android:gravity="center"
android:background="@drawable/button_shape2"
android:id="@+id/btnRetry"/>
<LinearLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp">
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="SCORE"
android:typeface="serif"
android:textSize="15dp"
android:gravity="center"/>
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:text="3"
android:textSize="30dp"
android:typeface="serif"
android:gravity="center"
android:textColor="@color/red2"
android:textStyle="bold"
android:id="@+id/tvScore"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>