title: How to check Shared Prefs when app loads?
body: <p>I am trying to remember a user's login credentials so that after he/she logs into the application the first time, each subsequent time, it automatically logs him/her in and starts the main activity.</p>
  
  <p>Right now I have two activities the Login activity and the Main app activity. When a user types in his/hers credentials, it verifies them against the DB on my server in an AsyncTask. Now I am using Shared Prefs to store the username and password and each time the app is started I want to read those prefs. If they are empty, then open the Login activity/screen, otherwise open the Main activity/screen. </p>
  
  <p>This works....but it takes about 3-5 seconds before it opens the Main activity. So it starts out on the Login activity and then after 3-5 seconds it goes to the Main activity. The following code is in the Login activity....where should I put it so that it does not open the Login activity unless the prefs are blank?  </p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.user_login);
  
      //keep screen on while activity is running
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  
      // Importing all assets like buttons, text fields
      inputEmail = (EditText) findViewById(R.id.loginEmail);
      inputPassword = (EditText) findViewById(R.id.loginPassword);
      btnLogin = (Button) findViewById(R.id.btnLogin);
      btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
      loginErrorMsg = (TextView) findViewById(R.id.login_error);
  
      //get username and password from preferences if they exist
      sharedPrefs = getSharedPreferences(PREFERENCES, getApplicationContext().MODE_PRIVATE);
  
      if (sharedPrefs.contains(UserId) &amp;&amp; sharedPrefs.contains(Password))
      {
          inputEmail.setText(sharedPrefs.getString(UserId, ""));
          inputPassword.setText(sharedPrefs.getString(Password, ""));
  
          new MyAsyncTask().execute(sharedPrefs.getString(UserId, ""),    
                                    sharedPrefs.getString(Password, ""));
      }
  
      // Login button Click Event
      btnLogin.setOnClickListener(new View.OnClickListener() {
  
          public void onClick(View view) {
              String email = inputEmail.getText().toString();
              String password = inputPassword.getText().toString(); 
  
              Editor editor = sharedPrefs.edit();
              editor.putString(UserId, email);
              editor.putString(Password, password);
              editor.commit();
  
              new MyAsyncTask().execute(email, password);
          }
      });
  }
  </code></pre>
  
  <p>Thanks for the help!</p>