title: Value of a global variable is reset after it is initialised in ValueEventListener
body: <p>I have a global variable named "bg". It gets initialised inside the ValueEventListener of the firebase database reference "myRef" </p>
  
  <p>But if I try to use the value of the variable outside the valueeventlistener block, the variable is empty. It's as if it's being reset.</p>
  
  <p>I am very new to android. Please let me know what's wrong.</p>
  
  <p>The code is as follows:</p>
  
  <pre><code>package com.example.tejasvi.donorfinder;
  
  import android.support.v4.app.FragmentActivity;
  import android.os.Bundle;
  import android.widget.Toast;
  
  import com.google.android.gms.maps.CameraUpdateFactory;
  import com.google.android.gms.maps.GoogleMap;
  
  import com.google.android.gms.maps.OnMapReadyCallback;
  import com.google.android.gms.maps.SupportMapFragment;
  import com.google.android.gms.maps.model.LatLng;
  import com.google.android.gms.maps.model.MarkerOptions;
  import com.google.firebase.auth.FirebaseAuth;
  import com.google.firebase.database.DataSnapshot;
  import com.google.firebase.database.DatabaseError;
  import com.google.firebase.database.DatabaseReference;
  import com.google.firebase.database.FirebaseDatabase;
  import com.google.firebase.database.ValueEventListener;
  
  public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
  
      private GoogleMap mMap;
      private FirebaseAuth mAuth;
  
      public String latlng[],bg;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
  
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_maps);
  
           mAuth = FirebaseAuth.getInstance();
  
           Bundle bundle = getIntent().getExtras();
  
           String s= bundle.getString("Phone");  
  
           String path="Active Requests/"+mAuth.getCurrentUser().getUid()+"/"+s+"/Blood Group";
  
          FirebaseDatabase database = FirebaseDatabase.getInstance();
          DatabaseReference myRef = database.getReference(path);   
  
  
          myRef.addListenerForSingleValueEvent(new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
  
                  bg=dataSnapshot.getValue().toString();
                  }
  
              @Override
              public void onCancelled(DatabaseError databaseError) {
                  Toast.makeText(MapsActivity.this, "Error", Toast.LENGTH_LONG).show();
  
              }
          });
  
          Toast.makeText(MapsActivity.this, bg, Toast.LENGTH_SHORT).show();  //bg is empty here
  
          SupportMapFragment mapFragment = (SupportMapFragment)    getSupportFragmentManager()
                  .findFragmentById(R.id.map);
          mapFragment.getMapAsync(this);
      }
  
      @Override
      public void onMapReady(GoogleMap googleMap) {
          mMap = googleMap;
  
          }
  }
  </code></pre>
  
  <p>The value of dataSnapshot.getValue() is "A+ve"</p>
  
  <p>The JSON file is as follows:</p>
  
  <pre><code>{
    "Active Requests": {
      "3R8XGIM5uSRv3sxQZ2sqqUnIjQ62": {
        "9849949799": {
          "Blood Group": "B+ve",
          "Date of Requirement": "3/1/17",
          "Direct or Replacement?": "Direct donation",
          "Donation Type": "Whole Blood",
          "For Self?": "Yes",
          "Hospital Name": "Shekhar Hospital",
          "Location": "12.945793000000002,77.56759339999999",
          "Name": "Poorna Chandra Tejasvi",
          "Units": "2"
        }
      },
      "YL5oVxmua4gktNM2TwwPavL1Tj32": {
        "123456789": {
          "Blood Group": "A+ve",
          "Date of Requirement": "1/1/17",
          "Direct or Replacement?": "Direct donation",
          "Donation Type": "Whole Blood",
          "For Self?": "No",
          "Hospital Name": "Columbia Asia Hospital - Hebbal",
          "Location": "13.0509869,77.5937892",
          "Recipient Name": "jssnxn",
          "Recipient Relation": "Married",
          "Units": "3"
        }
      }
    }
  }
  </code></pre>