title: How to make multi login with sharedpreferences?
body: <p>i had implement <a href="https://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/" rel="nofollow noreferrer">this tutorial</a> to using sharedpreferences, it's work for one user but when i do the same just add any string like that code it's just work too however it's always go to MainActivity not to Activity target what i want.</p>
  
  <p>See my code: <strong>SessionManager.java</strong></p>
  
  <pre><code>public class SessionManager {
  
  SharedPreferences pref;
  
  SharedPreferences.Editor editor;
  
  Context context;
  
  int PRIVATE_MODE = 0;
  
  private static final String PREF_NAME = "SSA";
  
  private static final String IS_LOGIN = "IsLoggedIn";
  
  public static final String KEY_NAME = "name";
  
  public static final String KEY_PETUGAS = "petugas";
  
  public static final String KEY_HOTEL = "hotel";
  
  public SessionManager(Context context) {
      this.context = context;
      pref = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
      editor = pref.edit();
  }
  
  public void createLoginSession(String name, String hotel) {
  
      editor.putBoolean(IS_LOGIN, true);
      editor.putString(KEY_NAME, name);
      editor.putString(KEY_HOTEL, hotel);
  
      editor.commit();
  }
  
  public void createPetugasSession(String petugas) {
      editor.putBoolean(IS_LOGIN, true);
      editor.putString(KEY_PETUGAS, petugas);
      editor.commit();
  }
  
  public void checkLogin() {
      if (!this.isLoggedIn()) {
          Intent intent = new Intent(context, LoginActivity.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(intent);
      }
  }
  
  public HashMap&lt;String, String&gt; getUserDetails() {
      HashMap&lt;String, String&gt; user_SSA = new HashMap&lt;String, String&gt;();
  
      user_SSA.put(KEY_NAME, pref.getString(KEY_NAME, null));
      user_SSA.put(KEY_PETUGAS, pref.getString(KEY_PETUGAS, null));
      user_SSA.put(KEY_HOTEL, pref.getString(KEY_HOTEL, null));
  
      return user_SSA;
  }
  
  public void logOutUser() {
      editor.clear();
      editor.commit();
  
      Intent intent = new Intent(context, LoginActivity.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      context.startActivity(intent);
  }
  
  
  public boolean isLoggedIn() {
      return pref.getBoolean(IS_LOGIN, false);
  }}
  </code></pre>
  
  <p><strong>LoginActivity.java</strong></p>
  
  <pre><code>public class LoginActivity extends AppCompatActivity {
  
  EditText username, password;
  Button login;
  
  AlertDialogManager alert = new AlertDialogManager();
  
  SessionManager session;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_login);
  
      session = new SessionManager(getApplicationContext());
  
      username = (EditText) findViewById(R.id.edt_username);
      password = (EditText) findViewById(R.id.edt_password);
  
  
      login = (Button) findViewById(R.id.btn_login);
      login.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
  
              String user = username.getText().toString();
              String pass = password.getText().toString();
  
              if (user.trim().length() &gt; 0 &amp;&amp; pass.trim().length() &gt; 0) {
  
                  if (user.equals("admin_ssa") &amp;&amp; pass.equals("ssa_rotary")) {
  
                      session.createLoginSession("Admin SSA", "Admin Hotel");
  
                      Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                      startActivity(intent);
                      finish();
                  } else if (user.equals("admin_petugas") &amp;&amp; pass.equals("petugas_rotary")) {
  
                      session.createPetugasSession("Admin Bandara");
  
                      Intent intent = new Intent(getApplicationContext(), ArrivedActivity.class);
                      startActivity(intent);
                      finish();
                  } else if (user.equals("admin_hotel") &amp;&amp; pass.equals("hotel_rotary")) {
                      session.createLoginSession("Admin SSA", "Admin Hotel");
  
                      Intent intent = new Intent(getApplicationContext(), HotelManagementActivity.class);
                      startActivity(intent);
                      finish();
  
                  } else {
                      alert.showAlertDialog(LoginActivity.this, "Login Failed..",
                              "username or password is incorect", false);
                  }
              } else {
                  alert.showAlertDialog(LoginActivity.this, "Login Failed..",
                          "Please enter username or password", false);
              }
          }
      });
  }}
  </code></pre>
  
  <p><strong>Manifest.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.kode.kreasi.rotarybarcode"&gt;
  
  &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  
  &lt;application
      android:name=".Utils.BaseApps"
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@drawable/rotarylogo"
      android:supportsRtl="true"
      android:theme="@style/NoActionBar"&gt;
      &lt;activity android:name=".MainActivity"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;activity
          android:name=".RegistrationMenuActivity"
          android:label="Registration"
          android:parentActivityName=".MainActivity"
          android:theme="@style/AppTheme" /&gt;
      &lt;activity
          android:name=".EventMenuActivity"
          android:label="Event"
          android:parentActivityName=".MainActivity"
          android:theme="@style/AppTheme" /&gt;
      &lt;activity android:name=".ScanQRCodeActivity" /&gt;
      &lt;activity
          android:name=".ManualActivity"
          android:label="Manual Absent"
          android:parentActivityName=".MainActivity"
          android:theme="@style/AppTheme" /&gt;
      &lt;activity android:name=".LoginActivity" /&gt;
      &lt;activity android:name=".ArrivedActivity" /&gt;
      &lt;activity android:name=".HotelManagementActivity" /&gt;
      &lt;activity
          android:name=".CheckInActivity"
          android:label="Check In"
          android:parentActivityName=".HotelManagementActivity"
          android:theme="@style/AppTheme"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.SEARCH" /&gt;
  
              &lt;category android:name="android.intent.category.DEFAULT" /&gt;
          &lt;/intent-filter&gt;
  
          &lt;meta-data
              android:name="android.app.searchable"
              android:resource="@xml/searchable" /&gt;
      &lt;/activity&gt;
      &lt;activity
          android:name=".SummaryActivity"
          android:label="Summary"
          android:parentActivityName=".HotelManagementActivity"
          android:theme="@style/AppTheme"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.SEARCH" /&gt;
  
              &lt;category android:name="android.intent.category.DEFAULT" /&gt;
          &lt;/intent-filter&gt;
  
          &lt;meta-data
              android:name="android.app.searchable"
              android:resource="@xml/searchable" /&gt;
      &lt;/activity&gt;
      &lt;activity
          android:name=".TourActivity"
          android:label="Tour"
          android:parentActivityName=".MainActivity"
          android:theme="@style/AppTheme" /&gt;
      &lt;activity
          android:name=".OnTheSpotActivity"
          android:label="@string/onthespot"
          android:parentActivityName=".HotelManagementActivity"
          android:theme="@style/AppTheme" /&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p></p>
  
  <p>I think my code it's simple, but it's not working with my mine because actually i'm new. Hope you reply guys, thankyou.</p>
  
  <p><a href="https://gph.is/2OzfnNt" rel="nofollow noreferrer">See the gif</a>
  In the first time i put the user + pass is the right target activity, but when i close the apps it's back to MainActivity not the target Activity.</p>