how to make instance variables updating in callback to happen in main thread? <p>This is the code that gets called when the GoogleApiClient gets created. All this does it retrieve the last location of the user. </p> <pre><code> //omitted code @Override public void onConnected(Bundle bundle) { Location location = LocationServices.FusedLocationApi.getLastLocation(mApiClient); if (location != null) { mLongitude = String.valueOf(location.getLongitude()); mLatitude = String.valueOf(location.getLatitude()); Log.d(TAG, mLongitude + "___" + mLatitude); //this one doesn't return null } else { Log.d(TAG, "Location is null"); LocationServices.FusedLocationApi.requestLocationUpdates(mApiClient, mLocationRequest, this); } } </code></pre> <p>This is for the google api client: </p> <pre><code>private void buildGoogleApiClient() { mApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); } </code></pre> <p>This snippet is from my onCreate() method:</p> <pre><code>buildGoogleApiClient(); Log.d(TAG, mLongitude + "___" + mLatitude); //this one returns null </code></pre> <p>So when the code gets run, i build a google api client and the onConnected method gets called. Inside of that method, i will update the mLongitude and mLatitude variables which are instance variables. However in the log, they return as null. I was wondering why this happened so i put another log INSIDE the if statement of my onConnected() method as you can see in the first block of code and this one successfully brings back the longitude and latitude. I dont know why its not updating the instance variables. I believe it's because the callback isn't being run on the main thread and so the log happens before the update. So I tried running the code in the main thread like this:</p> <pre><code>runOnUiThread(new Runnable() { @Override public void run() { buildGoogleApiClient(); } }); Log.d(TAG, mLongitude + "___" + mLatitude); </code></pre> <p>unfortunately this doesnt work either. I searched for things relating to this topic and tried implementing them in my code however none seem to work as i dont even know the real cause for this problem.</p> <p>Anyone know how to fix this?</p> <p>Thank you</p>