title: Retrofit: how to ensure second call waits for results from the first call
body: <p>I have an application where I do the login using a simple retrofit async call with Callback in the MyApplication.onCreate() method and store the userId that I get back as a global variable in MyApplication inside the retrofit callback.</p>
  
  <p>Now on the first Activity onCreate method. I use the userId that I retrieved from the login APi call in a separate API call to retrieve some user specific data.</p>
  
  <p>The problem is that in the Activity onCreate method when I want to call the second API call. the userId is still null since the first call in MyApplication didn't finish execution yet and the userId is not set yet.</p>
  
  <p>How do I ensure that the second call is executed only after a userId is returned from the first call and set properly.</p>
  
  <p>Please advice.</p>
  
  <p>MyApplication class</p>
  
  <pre><code>public class MyApplication extends Application {
  
  private String userId;
  
  @Override
  public void onCreate() {
      super.onCreate();
      // login, password retrieved here.
      retrofitClient.login(login, password, new Callback&lt;String&gt;() {
          @Override
          public void success(String userId, Response response) {
              setUserId(userId);
          }
  
          @Override
          public void failure(RetrofitError error) {
  
          }
      });
  }
  
  
  public class MainActivity extends FragmentActivity {
  
  private Context context;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      context = this;
      // do some logic.        
  String userId = ((MyApplication) getApplication()).getUserId();
  // NOW THE USERID is still NULL since the first call in the MyApplication      class is not done yet.
      retrofitClient.callSecondAPICall(userId, new Callback&lt;ProductResponse&gt;() {
  
          @Override
          public void success(MyResponse myResponse, Response response) {
      // do something.
          }
  
          @Override
          public void failure(RetrofitError error) {
  
          }
      });
  
  }
  </code></pre>