title: How can I keep track of a variable in android app?
body: <p>when user enter to the app he chose team 1 (Brazil)or team 2(Argentine) after that he chose yellow or red card then he chose a player after that in the first page it will show the player number and color of card.</p>
  <p><a href="https://i.stack.imgur.com/Wv1Ky.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p><em><strong>My Question is How can keep track of the number of card and instead of showing player number I show how many either yellow or red card does team have .</strong></em></p>
  <p><strong>CardActivity</strong></p>
  <pre><code>public class CardActivity extends AppCompatActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_card);
  }
  
  private void setCardData(int card) {
      SharedPreferences settings getApplicationContext().getSharedPreferences(&quot;MainThisApp&quot;, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putInt(&quot;card&quot;, card);
      editor.apply();
  
      Intent intent = new Intent(this, Myapp.game.NumberActivity.class);
      startActivity(intent);
  }
  
  public void setRedCard(View view) {
  
      ((Variable) this.getApplication()).setSomeVariable(&quot;card_yellow&quot;);
  
      setCardData(0);
  }
  
  public void setYellowCard(View view) {
  
      ((Variable) this.getApplication()).setSomeVariable(&quot;card_red&quot;);
  
      setCardData(1);
  }}
  </code></pre>
  <p><strong>MainActivity</strong></p>
  <pre><code>public class MainActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      SharedPreferences prefs = getApplicationContext().getSharedPreferences(&quot;MainThisApp&quot;, 0);
      int check = prefs.getInt(&quot;check&quot;, 0);
  
      String s = ((Variable) this.getApplication()).getSomeVariable();
      try{
          Integer number = Integer.valueOf(s);
          System.out.println(number);
      }
      catch (NumberFormatException ex){
          ex.printStackTrace();
      }
  
      if (check == 50) {
          TextView view = null;
          int country = prefs.getInt(&quot;country&quot;, -1);
          int card = prefs.getInt(&quot;card&quot;, -1);
  
          if (country == 0 &amp; card == 0) {
  
              view = findViewById(R.id.textView4);
          }
          if (country == 0 &amp; card == 1) {
  
              view = findViewById(R.id.textView3);
          }
          if (country == 1 &amp; card == 0) {
  
              view = findViewById(R.id.textView2);
  
          }
          if (country == 1 &amp; card == 1) {
  
              view = findViewById(R.id.textView);
          }
          view.setText(String.valueOf(prefs.getInt(&quot;number&quot;,1)));
      }
  
  }
  
  public void chooseBrasil(View view) {
      setCountryData(0);
  }
  
  private void setCountryData(int country) {
      SharedPreferences settings = getApplicationContext().getSharedPreferences(&quot;MainThisApp&quot;, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putInt(&quot;country&quot;, country);
      editor.apply();
      Intent intent = new Intent(this, CardActivity.class);
      startActivity(intent);
  }
  public void chooseArgentine(View view) {
      setCountryData(1);
  }
  </code></pre>
  <p><strong>NumberActivity</strong></p>
  <pre><code>public class NumberActivity extends AppCompatActivity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_number);
      }
  
  private void setNumberData(int number) {
      SharedPreferences settings = getApplicationContext().getSharedPreferences(&quot;MainThisApp&quot;, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putInt(&quot;number&quot;, number);
      editor.putInt(&quot;check&quot;, 50);
      editor.apply();
      Intent intent = new Intent(this, MainActivity.class);
      startActivity(intent);
  }
  
  public void setNumber1(View view) {
      setNumberData(1);
  }
  
  public void setNumber2(View view) {
      setNumberData(2);
  }
  
  public void setNumber3(View view) {
      setNumberData(3);
  }
  
  public void setNumber4(View view) {
      setNumberData(4);
  }
  
  public void setNumber5(View view) {
      setNumberData(5);
  }
  
  public void setNumber6(View view) {
      setNumberData(6);
  }
  
  public void setNumber7(View view) {
      setNumberData(7);
  }
  
  public void setNumber8(View view) {
      setNumberData(8);
  }
  </code></pre>