title: How to wait for a method with result callback to complete before continuing (Android)?
body: <p>So I am a noob at Android, and I'm writing a simple app that uses Google Fit to store the users fitness sessions and step count and then retrieve them.</p>
  
  <p>I have two methods, one that fetches all the sessions from a given date range from the cloud, the next method iterates through these and adds up the step count.</p>
  
  <p>Problem is, that although I call the the fetching method first, the result doesn't come back until after I've added the steps up, so step count is always zero.</p>
  
  <p>Here's my code:</p>
  
  <pre class="lang-java prettyprint-override"><code>private ArrayList&lt;&gt; results;
  
      @Override
      public ArrayList&lt;IndividualSession&gt; readAllSessions(Date dateFrom, Date dateTo) {
  
      /* I haven't included the following code in this question just to keep things clean, but here there was
          - The initialisation of the results ArrayList
          - Creating the calendar and date objects
          - Building the session read request
      */
  
      Fitness.SessionsApi.readSession(mGoogleApiClient, readRequest).setResultCallback(new ResultCallback&lt;SessionReadResult&gt;() {
          @Override
          public void onResult(SessionReadResult sessionReadResult) {
              for (Session session : sessionReadResult.getSessions()) {
                  List&lt;DataSet&gt; dataSets = sessionReadResult.getDataSet(session);
                  for (DataSet dataSet : dataSets) {
                      for (DataPoint dataPoint : dataSet.getDataPoints()) {
                      // Create new IndividualSession object, add data to it then add it to arraylist
                          IndividualSession individualSessionObject = new IndividualSession();
                          individualSessionObject.setFromDate(new Date(session.getStartTime(TimeUnit.SECONDS)));
                          individualSessionObject.setToDate(new Date(session.getEndTime(TimeUnit.SECONDS)));
                          individualSessionObject.setStepCount(dataPoint.getValue(Field.FIELD_STEPS).asInt());
                          results.add(individualSessionObject);
                      }
                  }
              }
              Log.i(TAG, "Number of sessions found while reading:  "+results.size());
          }
      });
      return results;
  }
  
  
  
  @Override
  public int getDaySteps(Date dateTo) {
      int stepCount = 0; // to be returned
  
      // Sort out the dates
      Calendar calFrom = Calendar.getInstance();
      calFrom.add(Calendar.HOUR, -24);
  
      // Get the sessions for appropriate date range
      ArrayList results =  readAllSessions(calFrom.getTime(), dateTo);
      Log.i(TAG, "Number of sessions found while trying to get total steps: "+results.size());
  
      // Iterate through sessions to get count steps
      Iterator&lt;IndividualSession&gt; it = results.iterator();
      while(it.hasNext())
      {
          IndividualSession obj = it.next();
          stepCount += obj.getStepCount();
      }
      return stepCount;
  }
  </code></pre>
  
  <p>This outputs</p>
  
  <pre><code>"Number of sessions found while trying to get total steps: 0"
  "Number of sessions found while reading:  8"
  </code></pre>