title: Android Studio quiz app saving score
body: <p>I'm currently making a flag quiz app, and want to add scores.  Below you can see a picture of it; each flag is a fragment, and when you press them, the right hand side allows you to submit an answer.</p>
  
  <p><a href="https://i.stack.imgur.com/lW4or.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lW4or.png" alt="enter image description here"></a></p>
  
  <p>When you enter the right answer, a "correct" message appears, and you can go to the next flag. I want a right answer to give a score of 1, and a wrong one (or when pressed skip or hint) to give a score of zero. At the end, the score is summarized. However, I'm unsure how to do this. I've created a ScoreActivity.java file where the scores will be registered (though it's currently empty), and I've tried writing the code shown below. </p>
  
  <p><strong>Play.java</strong></p>
  
  <pre><code>import android.content.Context;
  import android.content.Intent;
  import android.graphics.Color;
  import android.os.Bundle;
  import android.app.FragmentManager;
  import android.view.View;
  import android.app.Fragment;
  import android.app.FragmentTransaction;
  import android.view.View.OnClickListener;
  import android.view.LayoutInflater;
  import android.widget.Button;
  import android.widget.ImageButton;
  import android.view.ViewGroup;
  
  
  import layout.FragmentOne;
  import layout.FragmentTwo;
  import layout.FragmentThree;
  import layout.FragmentDefault;
  
  
  public class Play extends MainActivity {
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_play);
  
  
      final ImageButton imageBtn10 = (ImageButton) findViewById(R.id.imageButton10);
      final ImageButton imageBtn9 = (ImageButton) findViewById(R.id.imageButton9);
      final ImageButton imageBtn8 = (ImageButton) findViewById(R.id.imageButton8);
      final ImageButton imageBtn7 = (ImageButton) findViewById(R.id.imageButton7);
      final ImageButton imageBtn6 = (ImageButton) findViewById(R.id.imageButton6);
      final ImageButton imageBtn5 = (ImageButton) findViewById(R.id.imageButton5);
      final ImageButton imageBtn4 = (ImageButton) findViewById(R.id.imageButton4);
      final ImageButton imageBtn3 = (ImageButton) findViewById(R.id.imageButton3);
  
      FragmentManager fragMan = getFragmentManager();
      FragmentTransaction fragTrans = fragMan.beginTransaction();
      FragmentDefault fd = new FragmentDefault();
      fragTrans.replace(R.id.fragment_place, fd);
      fragTrans.commit();
  
      fragTrans.hide(new FragmentOne());
      fragTrans.hide(new FragmentTwo());
      fragTrans.hide(new FragmentThree());
  
      imageBtn10.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
                  FragmentManager fragMan = getFragmentManager();
                  FragmentTransaction fragTrans = fragMan.beginTransaction();
                  fragTrans.replace(R.id.fragment_place, new FragmentOne());
                  fragTrans.commit();
                  imageBtn10.setEnabled(false);
          }
  
      });
  
  
      imageBtn9.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              FragmentManager fragMan = getFragmentManager();
              FragmentTransaction fragTrans = fragMan.beginTransaction();
              fragTrans.replace(R.id.fragment_place, new FragmentTwo());
              fragTrans.commit();
          }
      });
  
      imageBtn8.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
                  FragmentManager fragMan = getFragmentManager();
                  FragmentTransaction fragTrans = fragMan.beginTransaction();
                  fragTrans.replace(R.id.fragment_place, new FragmentThree());
                  fragTrans.commit();
          }
      });
  
      imageBtn7.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              FragmentManager fragMan = getFragmentManager();
              FragmentTransaction fragTrans = fragMan.beginTransaction();
              fragTrans.replace(R.id.fragment_place, new FragmentThree());
              fragTrans.commit();
          }
      });
  
      imageBtn6.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              FragmentManager fragMan = getFragmentManager();
              FragmentTransaction fragTrans = fragMan.beginTransaction();
              fragTrans.replace(R.id.fragment_place, new FragmentThree());
              fragTrans.commit();
          }
      });
  
      imageBtn5.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              FragmentManager fragMan = getFragmentManager();
              FragmentTransaction fragTrans = fragMan.beginTransaction();
              fragTrans.replace(R.id.fragment_place, new FragmentThree());
              fragTrans.commit();
          }
      });
  
      imageBtn4.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              FragmentManager fragMan = getFragmentManager();
              FragmentTransaction fragTrans = fragMan.beginTransaction();
              fragTrans.replace(R.id.fragment_place, new FragmentThree());
              fragTrans.commit();
              //imageBtn4.setEnabled(false);
          }
      });
  
      imageBtn3.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              FragmentManager fragMan = getFragmentManager();
              FragmentTransaction fragTrans = fragMan.beginTransaction();
              fragTrans.replace(R.id.fragment_place, new FragmentThree());
              fragTrans.commit();
          }
      });
  
      Button button_score = (Button)findViewById(R.id.scoreButton);
      button_score.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Intent intentPlay = new Intent(Play.this, ScoreActivity.class);
              startActivity(intentPlay);
          }
      });
  
  
  
  }
  
  
  
  
  }
  </code></pre>
  
  <p><strong>FragmentOne.java</strong> (this is the fragment for the German flag)</p>
  
  <pre><code>import android.app.FragmentManager;
  import android.app.FragmentTransaction;
  import android.content.Context;
  import android.content.SharedPreferences;
  import android.net.Uri;
  import android.os.Bundle;
  import android.app.Fragment;
   import android.preference.PreferenceManager;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ImageButton;
  import android.widget.TextView;
  //import android.widget.TextView;
  
  
  public class FragmentOne extends Fragment {
  
  EditText theAnswer;
  Button ScoreButton;
  
  private EditText userAnswer;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      View v = inflater.inflate(R.layout.fragment_fragment_one, null);
      userAnswer = (EditText)v.findViewById(R.id.editText);
      final TextView tv = (TextView)v.findViewById(R.id.showCorrect);
      final TextView hintv = (TextView)v.findViewById(R.id.textHint);
  
  
      final Button submit = (Button)v.findViewById(R.id.submitBtn1);
      submit.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              String theAnswer = (userAnswer.getText().toString());
              if (theAnswer.equalsIgnoreCase("Germany")) {
                  //TextView tv = (TextView)v.findViewById(R.id.showCorrect);
                  tv.setText("Correct!");
  
              } else {
  
              }
              submit.setEnabled(false);
  
              // updateScore();
          }
      });
  
      final Button skip = (Button)v.findViewById(R.id.skipBtn);
      skip.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              submit.setEnabled(false);
          }
      });
  
      final Button hint = (Button)v.findViewById(R.id.hintBtn);
      hint.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              hintv.setText("The capital is Berlin \n The country is in Europe \n It starts with G... ");
              //submit.setEnabled(false);
          }
      });
  
  
      return v;
  
  
  }
  
  public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      ScoreButton = (Button)getView().findViewById(R.id.scoreButton);
  
  
      final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
      ScoreButton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              SharedPreferences.Editor editor = app_preferences.edit();
  
              if (userAnswer.isChecked()){
                  editor.putInt("answer_value", 1);
              }
              else {
                  editor.putInt("answer_value", 0);
              }
              editor.commit();
          }
      });
  }
  
  
  
  }
  </code></pre>
  
  <p>.isChecked() appears in red. Do I need to import something?</p>