title: Saving data using savedInstanceState
body: <p><strong>MainActivity.java</strong> </p>
  
  <pre><code> package com.jamesvuong.footballscorekeeper;
  
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
  import android.view.View;
  import android.widget.TextView;
  
  public class MainActivity extends AppCompatActivity {
      final int TOUCHDOWN_POINTS = 6;
      final int FIELD_GOAL_POINTS = 3;
      final int EXTRA_POINT_1_POINT = 1;
      final int EXTRA_POINT_2_POINTS = 2;
      final int SAFETY_POINTS = 2;
      String score_a , score_b;
      String TEXT_A , TEXT_B;
      int team_a_score;
      int team_b_score;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState); // Always call the superclass first
  
  
          // Check whether we're recreating a previously destroyed instance
          if (savedInstanceState != null) {
              // Restore value of members from saved state
              team_a_score = savedInstanceState.getInt(TEXT_A);
              team_b_score = savedInstanceState.getInt(TEXT_B);
          }
          else {
              // Probably initialize members with default values for a new instance
              team_a_score = 0;
              team_b_score = 0;
          }
          setContentView(R.layout.activity_main);
          displayTeamAScore(team_a_score);
          displayTeamBScore(team_b_score);
  
      }
  
  
  
      @Override
      public void onSaveInstanceState(Bundle savedInstanceState) {
          // Save the user's current game state
          savedInstanceState.putInt(TEXT_A,team_a_score);
          savedInstanceState.putInt(TEXT_B, team_b_score);
  
  
          // Always call the superclass so it can save the view hierarchy state
          super.onSaveInstanceState(savedInstanceState);
      }
      public void onRestoreInstanceState(Bundle savedInstanceState) {
          // Always call the superclass so it can restore the view hierarchy
          super.onRestoreInstanceState(savedInstanceState);
  
  
          // Restore state members from saved instance
          team_a_score = savedInstanceState.getInt(TEXT_A);
          team_b_score = savedInstanceState.getInt(TEXT_B);
      }
  
  
      /**
       * Update Scores
       */
      public void resetScores(View v) {
          team_a_score = 0;
          team_b_score = 0;
          displayTeamAScore(team_a_score);
          displayTeamBScore(team_b_score);
      }
  
      public void updateTeamScore(View v) {
          switch(v.getId()) {
              case R.id.team_a_touchdown:
                  team_a_score += TOUCHDOWN_POINTS;
                  break;
              case R.id.team_a_field_goal:
                  team_a_score += FIELD_GOAL_POINTS;
                  break;
              case R.id.team_a_extra_point_1:
                  team_a_score += EXTRA_POINT_1_POINT;
                  break;
              case R.id.team_a_extra_point_2:
                  team_a_score += EXTRA_POINT_2_POINTS;
                  break;
              case R.id.team_a_safety:
                  team_a_score += SAFETY_POINTS;
                  break;
  
              case R.id.team_b_touchdown:
                  team_b_score += TOUCHDOWN_POINTS;
                  break;
              case R.id.team_b_field_goal:
                  team_b_score += FIELD_GOAL_POINTS;
                  break;
              case R.id.team_b_extra_point_1:
                  team_b_score += EXTRA_POINT_1_POINT;
                  break;
              case R.id.team_b_extra_point_2:
                  team_b_score += EXTRA_POINT_2_POINTS;
                  break;
              case R.id.team_b_safety:
                  team_b_score += SAFETY_POINTS;
                  break;
  
              default:
                  break;
          }
  
          displayTeamAScore(team_a_score);
          displayTeamBScore(team_b_score);
      }
  
      /**
       * Display Scores
       */
      public void displayTeamAScore(int score) {
          TextView scoreView = (TextView) findViewById(R.id.team_a_score);
          scoreView.setText(String.valueOf(score));
      }
  
      public void displayTeamBScore(int score) {
          TextView scoreView = (TextView) findViewById(R.id.team_b_score);
          scoreView.setText(String.valueOf(score));
      }
  
  }
  </code></pre>
  
  <p><strong>Activity_main.XML</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:padding="10dp"
      tools:context="com.jamesvuong.footballscorekeeper.MainActivity"&gt;
  
  
      &lt;LinearLayout
          android:id="@+id/score_container"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="2"
          android:layout_marginBottom="10dp"
          android:orientation="horizontal"&gt;
  
          &lt;LinearLayout
              android:id="@+id/team_a_score_container"
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="6"
              android:orientation="vertical"&gt;
  
              &lt;TextView
                  android:id="@+id/team_a_label"
                  android:text="Team A"
                  style="@style/TeamLabelStyle"/&gt;
  
              &lt;TextView
                  android:id="@+id/team_a_score"
                  android:text="0"
                  android:saveEnabled="true"
                  style="@style/ScoreTextStyle"/&gt;
  
          &lt;/LinearLayout&gt;
  
          &lt;LinearLayout
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"&gt;
              &lt;TextView
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:text="-"
                  android:gravity="center"
                  android:textStyle="bold"
                  android:textSize="30sp"/&gt;
          &lt;/LinearLayout&gt;
  
          &lt;LinearLayout
              android:id="@+id/team_b_score_container"
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="6"
              android:orientation="vertical"&gt;
  
              &lt;TextView
                  android:id="@+id/team_b_label"
                  android:text="Team B"
                  style="@style/TeamLabelStyle"/&gt;
  
              &lt;TextView
                  android:id="@+id/team_b_score"
                  android:text="0"
                  android:saveEnabled="true"
                  style="@style/ScoreTextStyle"/&gt;
  
          &lt;/LinearLayout&gt;
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="1dp"
          android:background="@android:color/darker_gray"
          android:layout_marginBottom="10dp"&gt;
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:id="@+id/scoring_buttons_container"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="4"
          android:layout_gravity="center"
          android:orientation="vertical"&gt;
  
          &lt;LinearLayout
              android:id="@+id/touchdown_container"
              style="@style/ScoreTypeButtonContainerStyle"&gt;
  
              &lt;Button
                  android:id="@+id/team_a_touchdown"
                  style="@style/ScoreButtonStyle"
                  android:text="+6"
                  android:onClick="updateTeamScore"/&gt;
  
              &lt;TextView
                  android:text="Touchdown"
                  style="@style/ScoreTypeLabelStyle"/&gt;
  
              &lt;Button
                  android:id="@+id/team_b_touchdown"
                  style="@style/ScoreButtonStyle"
                  android:text="+6"
                  android:onClick="updateTeamScore"/&gt;
  
          &lt;/LinearLayout&gt;
  
          &lt;LinearLayout
              android:id="@+id/field_goal_container"
              style="@style/ScoreTypeButtonContainerStyle"&gt;
  
              &lt;Button
                  android:id="@+id/team_a_field_goal"
                  style="@style/ScoreButtonStyle"
                  android:text="+3"
                  android:onClick="updateTeamScore"/&gt;
  
              &lt;TextView
                  android:text="Field Goal"
                  style="@style/ScoreTypeLabelStyle"/&gt;
  
              &lt;Button
                  android:id="@+id/team_b_field_goal"
                  style="@style/ScoreButtonStyle"
                  android:text="+3"
                  android:onClick="updateTeamScore"/&gt;
  
          &lt;/LinearLayout&gt;
  
          &lt;LinearLayout
              android:id="@+id/extra_point_container"
              style="@style/ScoreTypeButtonContainerStyle"&gt;
  
              &lt;Button
                  android:id="@+id/team_a_extra_point_1"
                  style="@style/ScoreButtonStyle"
                  android:text="+1"
                  android:onClick="updateTeamScore"/&gt;
  
              &lt;Button
                  android:id="@+id/team_a_extra_point_2"
                  style="@style/ScoreButtonStyle"
                  android:text="+2"
                  android:onClick="updateTeamScore"/&gt;
  
              &lt;TextView
                  style="@style/ScoreTypeLabelStyle"
                  android:text="Extra Point" /&gt;
  
              &lt;Button
                  android:id="@+id/team_b_extra_point_1"
                  style="@style/ScoreButtonStyle"
                  android:text="+1"
                  android:onClick="updateTeamScore"/&gt;
  
              &lt;Button
                  android:id="@+id/team_b_extra_point_2"
                  style="@style/ScoreButtonStyle"
                  android:text="+2"
                  android:onClick="updateTeamScore"/&gt;
  
          &lt;/LinearLayout&gt;
  
          &lt;LinearLayout
              android:id="@+id/safety_container"
              style="@style/ScoreTypeButtonContainerStyle"&gt;
  
              &lt;Button
                  android:id="@+id/team_a_safety"
                  style="@style/ScoreButtonStyle"
                  android:text="+2"
                  android:onClick="updateTeamScore"/&gt;
  
              &lt;TextView
                  style="@style/ScoreTypeLabelStyle"
                  android:text="Safety"/&gt;
  
              &lt;Button
                  android:id="@+id/team_b_safety"
                  style="@style/ScoreButtonStyle"
                  android:text="+2"
                  android:onClick="updateTeamScore"/&gt;
  
          &lt;/LinearLayout&gt;
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="1dp"
          android:background="@android:color/darker_gray"
          android:layout_marginBottom="10dp"&gt;
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" /&gt;
      &lt;/LinearLayout&gt;
  
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:orientation="vertical"&gt;
  
          &lt;Button
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#333333"
              android:onClick="resetScores"
              android:text="Reset"
              android:textColor="#FFFFFF" /&gt;
  
      &lt;/LinearLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>I want to save some values, like score of both the team, so that even after the app is killed(not running anymore in background) the scores are saved and can be retained back when the user again opens the app.I have tried using 'savedInstanceState' but its not working. Please help. I am a beginner in android development.</p>