title: Clear all SharedPreferences android
body: <p>Iam saving my user login details into my SharedPreferences file. The problem is when I try to clear the ShredPreferences, the details dont clear. When the next user logs in his details are not shown.</p>
  
  <p>Here is my code:-</p>
  
  <p>Save.java</p>
  
  <pre><code>sessionManager.createLoginSession(username, deviceId,  name );
  </code></pre>
  
  <p>SessionManagement.java</p>
  
  <p>SharedPreferences pref;</p>
  
  <pre><code>   // Editor for Shared preferences
          Editor editor;
  
          // Context
          Context _context;
  
          // Shared pref mode
          int PRIVATE_MODE = 0;
  
          // Sharedpref file name
          private static final String PREF_NAME = "UserDetails";
  
      public SessionManagement(Context context)
          {
              this._context = context;
              pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
              editor = pref.edit();
          }
  
          public void createLoginSession(String emailId, String deviceid,String usersname)
          {
              // Storing login value as TRUE
              editor.putBoolean(IS_LOGIN, true);
  
              editor.putBoolean(KEY_DEVICEREGISTERED, true);
              editor.putString(KEY_EMAILID, emailId);
  
              editor.putString(KEY_DEVICEiD, deviceid);
  
              editor.putString(KEY_USERSNAME, usersname);
  
  
              // commit changes
              editor.commit();
          }
  
          /**
           * Get stored session data
           * */
          public HashMap&lt;String, String&gt; getUserDetails()
          {
              HashMap&lt;String, String&gt; user = new HashMap&lt;String, String&gt;();
  
              user.put(KEY_EMAILID, pref.getString(KEY_EMAILID, null));
  
              user.put(KEY_DEVICEiD, pref.getString(KEY_DEVICEiD, null));
  
              user.put(KEY_USERSNAME, pref.getString(KEY_USERSNAME, null));
  
  
              // return user
              return user;
          }
          /**
           * Check login method wil check user login status
           * If false it will redirect user to login page
           * Else won't do anything
           * */
          public void checkLogin()
          {
              // Check login status
              if(!this.isLoggedIn())
              {
                  // user is not logged in redirect him to Login Activity
                  Intent i = new Intent(_context, Login.class);
                  // Closing all the Activities
                  i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  
                  // Add new Flag to start new Activity
                  i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  
                  // Staring Login Activity
                    _context.startActivity(i);
              }
  
          }
  
         // This function clears all session data and redirect the user to LoginActivity
          /**
               * Clear session details
               * */
              public void logoutUser()
              {
                  // Clearing all data from Shared Preferences
                  //editor.clear();
  
  
  
                  editor.remove(KEY_DEVICEiD);
                  editor.remove(KEY_EMAILID);
  
                  editor.remove(KEY_USERSNAME);
  
                  editor.commit();
  
                  // After logout redirect user to Loing Activity
                  Intent i = new Intent(_context, Login.class);
                  // Closing all the Activities
                  i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  
                  // Add new Flag to start new Activity
                  i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  
                  // Staring Login Activity
                  _context.startActivity(i);
              }
  
              public boolean isLoggedIn()
              {
                  return pref.getBoolean(IS_LOGIN, false);
              }
  </code></pre>
  
  <p>Logout.java</p>
  
  <pre><code>SessionManagement session = new SessionManagement(getApplicationContext());
          session.logoutUser();
  </code></pre>
  
  <p>finish();</p>