title: boolean flag is always false
body: <p>I am trying to interact with an API and see whether the user exists on that API by checking the JSON Response by comparing current email (stored in SharedPrefs) with the emails returned from API. If the user exist, a flag is set <strong>true</strong> so that the app doesn't send a POST request to save the new user and if it is <strong>false</strong>, the user gets saved in API.</p>
  
  <p>So, this is the <strong>UEC (UserExistenceChecker) class</strong></p>
  
  <pre><code>public class UEC extends AppCompatActivity {
  
  List&lt;SavePlace&gt; userInfo;
  
  String name;
  boolean flag;
  SharedPreferences sharedPref;
  
  public UEC(SharedPreferences sharedPref){
      this.sharedPref = sharedPref;
  }
  
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  }
  
  public boolean checkIfUserExists() {
  
      email = sharedPref.getString("userEmail", "");
  
      Retrofit retrofitCheckUser = ApiClientSavePlace.getClient();
      ApiInterfaceSavePlace apiInterfaceSavePlace = retrofitCheckUser.create(ApiInterfaceSavePlace.class);
      final Call&lt;List&lt;SavePlace&gt;&gt; checkUser = apiInterfaceSavePlace.getSavePlaces();
  
      checkUser.enqueue(new Callback&lt;List&lt;SavePlace&gt;&gt;() {
          @Override
          public void onResponse(Call&lt;List&lt;SavePlace&gt;&gt; call, Response&lt;List&lt;SavePlace&gt;&gt; response) {
              userInfo = response.body();
              try {
                  if(userInfo.size()!=0){
                      for (int i = 0; i &lt;= userInfo.size(); i++) {
                          String emailReturned = userInfo.get(i).getEmail();
                          Log.d("response", "email returned: " + emailReturned);
                          Log.d("sharedpref", "email: " + email);
                          if (emailReturned.equals(email)) {
                          Log.d("response:", "email match?: " + emailReturned.equals(email));
                              flag = true;
                              SharedPreferences.Editor editor = sharedPref.edit();
                              editor.putInt("userID", userInfo.get(i).getId());
                              Log.d("ID returned", String.valueOf(userInfo.get(i).getId()));
                              editor.apply();
                              break;
                          }
                      }
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
  
          @Override
          public void onFailure(Call&lt;List&lt;SavePlace&gt;&gt; call, Throwable throwable) {
              Log.d("RESPONSE", "FAILED CHECKING USER ID/SOMETHING HAPPENED");
          }
      });
      return flag;
  }
  
  }
  </code></pre>
  
  <p>In this class, I have made a <strong>boolean flag</strong> with default value <strong>false</strong> </p>
  
  <p>and this is how I call the method <strong>checkIfUserExists()</strong> from <strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity{
  
  SharedPreferences sharedPref = 
  PreferenceManager.getDefaultSharedPreferences(getBaseContext());
  
      UEC uec = new UEC(sharedPref);
      boolean userExists = uec.checkIfUserExists();
      if (userExists) {
          Log.d("USERSTATUS", String.valueOf(sharedPref.getInt("userID", 0)));
      } else {
          Log.d("USERSTATUS", "FALSE:DOESNT EXIST");
          Log.d("USERSTATUS", String.valueOf(sharedPref.getInt("userID", 0)));
      }
  }
  </code></pre>
  
  <p>Now, the problem is that, according to the logs, <strong>else</strong> condition is always <strong>true</strong> because the <strong>flag</strong> is always <strong>false</strong> even though I am setting it <strong>true</strong> in <strong>checkIfUserExists()</strong> method. </p>
  
  <p>and the interesting thing about <strong>logs</strong> is that this</p>
  
  <pre><code>05-13 15:27:54.278 1613-1613/xyz.gautamhans.locus D/USERSTATUS: FALSE:DOESNT 
  EXIST
  05-13 15:27:54.278 1613-1613/xyz.gautamhans.locus D/USERSTATUS: 12
  </code></pre>
  
  <p>comes first and then this comes in logs after above log</p>
  
  <pre><code>05-13 15:27:55.746 1613-1613/xyz.gautamhans.locus D/response: email 
  returned: some-email@gmail.com
  05-13 15:27:55.749 1613-1613/xyz.gautamhans.locus D/sharedpref: email: 
  some-email@gmail.com
  05-13 15:27:55.749 1613-1613/xyz.gautamhans.locus D/response: email 
  returned: some-email@gmail.com
  05-13 15:27:55.749 1613-1613/xyz.gautamhans.locus D/sharedpref: email: 
  some-email@gmail.com
  05-13 15:27:55.749 1613-1613/xyz.gautamhans.locus D/response: email match?: 
  true
  05-13 15:27:55.749 1613-1613/xyz.gautamhans.locus D/ID returned: 12
  </code></pre>
  
  <p>which means that it detected the email and set the sharedpref
  but the flag is still false. </p>