title: How to save Highscore after app closed?
body: <p>I want to save the highscore, even after App is closed, on how many times I clicked the Imagebutton. I want to see the highscore the whole time the app is running
  But when I try my code the App crashed every time and I dont know where I made a mistake.</p>
  
  <p>EDIT: Now the App doesn't crach but the Highscore ist always Zero!
  EDIT 2: Got it now! This is the code that works for me.</p>
  
  <pre><code> public class MainActivity extends AppCompatActivity {
  private ImageView cirlce;
  private TextView Zahl;
  private ImageButton reset;
  int counter = 0;
  int readHighscore;
  
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      cirlce = (ImageView) findViewById(R.id.imageView);
      Zahl = (TextView) findViewById(R.id.Zahl);
      Zahl.setText(Integer.toString(counter));
      TextView highscore = (TextView) findViewById(R.id.highscore);
      highscore.setText(Integer.toString(readHighscore));
  
   cirlce.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              counter++;
              Zahl.setText(Integer.toString(counter));
  
  
  
     cirlce.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              counter++;
              Zahl.setText(Integer.toString(counter));
              TextView highscore = (TextView) findViewById(R.id.highscore);
              highscore.setText(Integer.toString(readHighscore));
  
              if (readHighscore &gt; counter) {
                  highscore.setText(Integer.toString(readHighscore));
  
              }
              else {
                  readHighscore = counter;
                  highscore.setText(Integer.toString(counter));
  
              }
  </code></pre>
  
  <p>SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
          readHighscore = prefs.getInt("highscore", 0);</p>
  
  <pre><code>    if (readHighscore &gt; counter) {
          highscore.setText(Integer.toString(readHighscore));
  
      }
      else {
          readHighscore = counter;
          highscore.setText(Integer.toString(counter));
  
      }
  }
  
  
  @Override
  protected void onPause() {
      super.onPause();
  
      SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
      SharedPreferences.Editor editor = prefs.edit();
      editor.putInt("highscore", readHighscore);
  
      editor.commit();
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is my XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/x"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"&gt;
  
      &lt;ImageView
          android:id="@+id/imageView"
          android:layout_width="348dp"
          android:layout_height="346dp"
          app:srcCompat="@drawable/cirlce"
          tools:layout_editor_absoluteX="18dp"
          tools:layout_editor_absoluteY="82dp"
          android:layout_centerVertical="true"
          android:layout_centerHorizontal="true" /&gt;
  
      &lt;TextView
          android:id="@+id/Zahl"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@null"
          android:textSize="100sp"
          android:typeface="sans"
          android:layout_centerInParent="true"/&gt;
  
  
      &lt;TextView
          android:id="@+id/highscore"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignTop="@+id/imageView"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="42dp"
          android:text="@string/Highscore" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>