title: Firebase on Android - how to make event handler for data retrieval fire when I need it to?
body: <p>I am new to Firebase and need some help with a query to retrieve data from a table. I am currently able to access and retrieve the data that I need from firebase, however, the timing is the problem I am having an issue with. </p>
  
  <p>From everything I've seen, the firebase database requires me to add event listeners to the Query or DatabaseReference objects. I am trying to download the contents of a node called "questions" before a method to display the question contents is called, however, I cannot control the timing of the firing of the event which downloads the data, and as a result my display method is always called before the firebase event fires.</p>
  
  <p>How can I execute a query when I want, and be sure it will be completed before a certain section of my code executes? I am used to traditional RDBs where you execute a query and get its results and then move forward with your logic. The need to use an event handler with firebase is what I am having a hard time with. I have even tried moving the definition of the firebase reference object and the event handler into onCreate() and moved the code that calls my display method into onStart() without any success - same problem. The data I am trying to get does not change so I only need to download it once at the beginning to have available for the display method.</p>
  
  <p>Here is an image of my "questions" node which is a child of the root.
  image of the child "questions" node on my firebase DB</p>
  
  <p><a href="https://i.stack.imgur.com/j0h3D.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/j0h3D.png" alt="enter image description here"></a></p>
  
  <p>Here is my code:</p>
  
  <pre><code>    @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      // Get Firebase DB reference
      firebase = FirebaseDatabase.getInstance();
      fdbRef = firebase.getReference("questions");
  
      // [START Question_event_listener]
      fdbRef.addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
              // Get Questions object and use the values to update the UI
              objQuestions = dataSnapshot.getValue();
              Log.w("Firebase:", "In Firebase ValueEventListener");
          }
  
          @Override
          public void onCancelled(DatabaseError databaseError) {
              // Getting Questions failed, log a message
              Log.w("Firebase Error:", "onCancelled:", databaseError.toException());
              Toast.makeText(ReviewActivity.this, "Failed to load question!", Toast.LENGTH_SHORT).show();
          }
      });
      //. . . remaining onCreate logic removed for simplicity
  } //end of onCreate
  
  @Override
  public void onStart() {
      super.onStart();
  
      // I moved this logic from onCreate to onStart but did not help...
      // Firebase retrieve must execute before I call any of these
      if (list_type == MainActivity.LIST_UNREVIEWED_DOCS)
          displayNewReviewForm();
      else if (list_type == MainActivity.LIST_REVIEWS)
          displayCompletedReview();
      else // (list_type == MainActivity.LIST_DRAFTS)
          displayDraftReview();
  }
  </code></pre>
  
  <p>Other alternatives if I can't get this resolved may be to move this retrieve logic to the prior Activity in my sequence and pass the retrieved data as an extra to this activity - but that seems really silly to have to do such a thing. I would think I should be able to get data from a DB when I need it... not when it feels like giving it to me.</p>
  
  <p>I appreciate any help getting me past this issue.</p>