title: Storing an int value using Shared preferences
body: <p>I have an int "flag" variable , which will have two possible int values , 0 &amp; 1.</p>
  
  <p>Main theme of the app here is : Ask user yes or no?</p>
  
  <p>If user selects YES, assign int=1.
  else if Selects No,  assign int=0;</p>
  
  <p>I am achieving this using:</p>
  
  <pre><code>public static int flag;
  
  
      @Override
  public void onClick(View v) {
      // TODO Auto-generated method stub
      switch (v.getId()) {
      case R.id.flag0:
          flag=0;
          System.err.println("Flag : " + flag);
          break;
      case R.id.flag1:
          flag=1;
          System.err.println("Flag : " + flag);
          break;
      default:
          break;
      }
  }
  </code></pre>
  
  <p>My problem is here: it is ok &amp; saving the preference upto my Application is running either foreground or background.</p>
  
  <p>and once if we close/kill the application, all the stored preferences are being cleared i.e., setting to the default value of Zero.</p>
  
  <p>As I just started working on STORAGE Options, I just tried to implement SHARED PREFERENCES, which still having the same above problem . the method I used to store the int value using Shared preferences is ;</p>
  
  <p><strong>Edit: I just tried the below answers but in vain &amp; here is my total code I am posting it , will be helpful for you to test(Maybe)</strong></p>
  
  <p>xml File:</p>
  
  <pre><code>&lt;RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" &gt;
  
  &lt;TextView
      android:id="@+id/text"
      android:textSize="30sp"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/hello_world" /&gt;
  
  &lt;Button android:id="@+id/flag0"
      android:layout_marginTop="10dp"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/text"
      android:text="set to 0"/&gt;
  
  &lt;Button android:id="@+id/flag1"
      android:layout_marginTop="10dp"
      android:layout_below="@+id/text"
      android:layout_toRightOf="@+id/flag0"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="set to 1"/&gt;
  
  &lt;Button android:id="@+id/test0"
      android:layout_marginTop="10dp"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/flag1"
      android:text="Test for 0"/&gt;
  </code></pre>
  
  <p></p>
  
  <p>MainActivity:</p>
  
  <pre><code>public class MainActivity extends Activity implements OnClickListener {
  TextView textView;
  Button bt1,bt2,testbt1;
  public static int flag;
  SharedPreferences pref;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      textView = (TextView) findViewById(R.id.text);
      bt1 = (Button) findViewById(R.id.flag0);
      bt2 = (Button) findViewById(R.id.flag1);
      testbt1 = (Button) findViewById(R.id.test0);
  
      bt1.setOnClickListener(this);
      bt2.setOnClickListener(this);
      testbt1.setOnClickListener(this);
  
  
      pref=getSharedPreferences("Sai", MODE_PRIVATE);
      pref.getInt("lang_us", 0);
      System.err.println("Flag Value in Main method : " + flag);
  }
  public void saveFlag(){
  
      pref=getSharedPreferences("Sai",MODE_PRIVATE);
      Editor editor = pref.edit();
      editor.putInt("lang_us", flag);
      editor.commit();
  
  }
  @Override
  public void onClick(View v) {
      // TODO Auto-generated method stub
      switch (v.getId()) {
      case R.id.flag0:
          flag=0;
          saveFlag();
          System.err.println("Flag : " + flag);
          break;
      case R.id.flag1:
          flag=1;
          saveFlag();
          System.err.println("Flag : " + flag);
          break;
      case R.id.test0:
          System.err.println("Flag : " + flag);
          if (flag == 0) {
              textView.setText("flag is zero");
          } else {
              textView.setText("flag is not zero");
          }
  
          break;
  
      default:
          break;
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Where is it going wrong?</p>