title: how to sign in automatically using sharedpreferences in android
body: <p>i have splash activity that will be directed to login activity, if login is true, then the user will be direct to home, but when the user press back in home, application will be closing automatically.
  but the problem is when user open  application, they must be login again, even they not logout before. so how to direct splash into home activity? i have read, to handle it i must used sharedpreferences so i have to used it, but i don't how to handle login automatically using it.
  this is my spash:</p>
  
  <pre><code>public class SplashEpolicy extends Activity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  
      //hide title bar
      BasicDisplaySettings.toggleTaskBar(SplashEpolicy.this, false);
      //show status bar
      BasicDisplaySettings.toggleStatusBar(SplashEpolicy.this, true);
  
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.splash_epolicy);
  
      Thread timer = new Thread() {
          public void run() {
              try {
                  //this is configuration for how long the splash will be shown.
                  //
                  int time = Integer.parseInt(getResources().getString(R.string.splashTransTime));
                  sleep(time);
              } catch (InterruptedException e) {
                  // TODO: handle exception
                  e.printStackTrace();
              } finally {
                  //line below is for direct new activity that will be run after 
                  //current activity finished.
                  Intent intent = new Intent(SplashEpolicy.this,EpolicyMainActivity.class);
                  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
                  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  startActivity(intent);      
                  finish();
              }
          }
      };
      timer.start();      
  }
  </code></pre>
  
  <p>this is my login activity that containing sharedpreferences:</p>
  
  <pre><code>                String KEY = jo.getString("key");
                  SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
                  SharedPreferences.Editor editor = settings.edit();
                  editor.putString("etUser", strUser);
                  editor.putString("key",KEY);
                  editor.commit();
                  System.out.println(KEY+""+"key"+""+"etUser");
  
                  Dialog.dismiss();
                  Intent i = new Intent(LoginActivity.this,EpolicyMainTab.class);
                  startActivity(i);
  
          }
          }catch (JSONException e) {
              e.printStackTrace();
          }
  </code></pre>
  
  <p>so this is my Home (EpolicyListPolisActivity):</p>
  
  <pre><code>    public class EpolicyListPolis extends ListActivity {
      static String PEMEGANG="PEMEGANG";
      static String POLIS="POLIS";
      static String STATUS="STATUS";
      static String TERTANGGUNG="TERTANGGUNG";
      String KEY, strUser;
      List NasabahList= new ArrayList();
      private ProgressDialog Dialog;
      private String strStoragePath = "",
              strNameFileDiv = "";
      ImageView LogoutButton;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setTitle(R.string.Nomor_Polis);
  
  
          setContentView(R.layout.epolicy_list_polis);
          SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
          strUser= settings.getString("etUser", null);
          KEY = settings.getString("key", null);
          LogoutButton=(ImageView)findViewById(R.id.LogoutButton);
  
          new NasabahAsyncTask().execute();
  
          LogoutButton.setOnClickListener(new OnClickListener(){
              public void onClick(View v) {
                  dialogSignOut();
              }
          });
              }
  public class NasabahAsyncTask extends AsyncTask&lt;Void, Void, String&gt;{
      String url = ("http://www.example.com?i="+strUser+"&amp;k="+KEY);
      public NasabahAsyncTask() {
          this.url=url;
          // TODO Auto-generated constructor stub
      }
  
      protected void onPreExecute() {
          super.onPreExecute();   
          Dialog = ProgressDialog.show(EpolicyListPolis.this, "", "Melakukan Pengambilan Data");
      }
  
  @Override
  protected String doInBackground(Void... params) {
      String result="";
      try {
          result=Connection.get(url);
      }catch (Exception e){
          result=" ";
          Log.d("test viewer",e.getMessage());
      }
      return result;
  }
  
  @Override
  protected void onPostExecute(String result) {
      super.onPostExecute(result);
      fetchResponse(result.replace("\n", "").trim());
  
      ListAdapter adapter = new SimpleAdapter(
              EpolicyListPolis.this, NasabahList, R.layout.epolicy_polis_listitem,
              new String[] {POLIS, PEMEGANG, TERTANGGUNG, STATUS}, new int [] {R.id.polis, R.id.pemegang, R.id.tertanggung, R.id.status});
  
          setListAdapter(adapter);
      Dialog.dismiss();
      ListView lv = getListView();
  
      lv.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                  int position, long id) {
  
              String POLIS = ((TextView)view.findViewById(R.id.polis)).getText().toString();
  
              SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
              SharedPreferences.Editor editor = settings.edit();
              editor.putString("POLIS", POLIS);
              editor.commit();
              }
  }
  
  private void fetchResponse(String result) {
      // TODO Auto-generated method stub
      if (!result.equals("")) {
          try {
              JSONArray jsonArray = new JSONArray(result);
              for (int i = 0; i &lt; jsonArray.length(); i++) {
  
                  JSONObject jsonObject = jsonArray.getJSONObject(i);
                  HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
                  if (jsonObject.has("PEMEGANG")) 
                      map.put("PEMEGANG", jsonObject.get("PEMEGANG").toString());
                  if (jsonObject.has("POLIS")) 
                      map.put("POLIS", jsonObject.get("POLIS").toString());
                  if (jsonObject.has("STATUS")) 
                      map.put("STATUS", jsonObject.get("STATUS").toString());
                  if (jsonObject.has("TERTANGGUNG")) 
                      map.put("TERTANGGUNG", jsonObject.get("TERTANGGUNG").toString());
                  NasabahList.add(map);
                  System.out.println("json oke");
  }
          }catch (JSONException e) {
  
              e.printStackTrace();
          }
  
          }
      }
  @Override
  public void onBackPressed() {
      dialogExit();
  }
  public void dialogExit()
  {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setMessage("do you want to exit?")
             .setCancelable(false)
             .setPositiveButton("Ya", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                     Intent itSplashEnd = new Intent(EpolicyListPolis.this, SplashEnd.class);
                     //setIntent.addCategory(Intent.CATEGORY_HOME);
                     itSplashEnd.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
                     itSplashEnd.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                     startActivity(itSplashEnd);
                     finish();
                     System.exit(0);
                 }
             })
             .setNegativeButton("no", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                      dialog.cancel();
                 }
             });
      AlertDialog alert = builder.create();
      alert.show();
  }
  
  public void dialogSignOut()
  {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setMessage("Sign out?")
             .setCancelable(false)
             .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                 // what will i do here?
  
                     Intent itSignOut = new Intent(EpolicyListPolis.this, EpolicyMainActivity.class);
                     itSignOut.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                     startActivity(itSignOut);
                     finish();
                 }
             })
             .setNegativeButton("no", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                      dialog.cancel();
                 }
             });
      AlertDialog alert = builder.create();
      alert.show();
  }
  
      }
  </code></pre>
  
  <p>so how to sign in automatically into home without going to login activity again using sharedpreferences? and how to clear data that saved in sharedpreferences every time user sign out?</p>