Search code examples
androidandroid-fragmentsgoogle-api-client

GoogleAPIClient not connecting after phone rotated


I have a class for creating Geofences containing a GoogleApiClient, which is currently accessed by two activities (created by following this tutorial). I set up the class as a non-UI Fragment, because as far as I can tell (please correct me if I'm wrong!) GoogleApiClient needs to be attached to a lifecycle, and to be able to connect it in multiple locations I can't embed it into the activity.

Within the activities, I create the Fragment;

private GeofenceUtilityFragment geofenceUtils = new GeofenceUtilityFragment();

And add it to the activity;

if (savedInstanceState == null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().add(geofenceUtils, getString(R.string.fragment_key_geofence_utils)).commit();
}

When the user first navigates to the activity everything works a treat. However, when the app is rotated (or any other event which causes a redraw occurs, presumably), the GoogleApiClient returns a null object reference.

Method call;

private void setGeofences(){
    if (!mGoogleApiClient.isConnected()){
        Toast.makeText(activityContext, "not connected", Toast.LENGTH_SHORT).show();
        return;
    }

What makes it all really confusing though, is that after an rotate, the onConnected callback triggers, so the API client is reconnecting, but for some reason when I try to access it I get a null.

Here's the Log statement showing the two (with some additional fluff in the middle cut out)

0387/com.example.android.bentheredonethat D/GeofenceUtilityFragment: onConnected has just been called

[...]

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.android.gms.common.api.GoogleApiClient.isConnected()' on a null object reference
                                                                                      at geolocationtools.GeofenceUtilityFragment.setGeofences(GeofenceUtilityFragment.java:179)
                                                                                      at geolocationtools.GeofenceUtilityFragment.callGeofenceRoutine(GeofenceUtilityFragment.java:220)
                                                                                      at com.example.android.bentheredonethat.RouteDetailsActivity.startRouteButtonHandler(RouteDetailsActivity.java:321)

Pretty bamboozled on this one, so any help would be greatly appreciated! If there's anything else I could provide to help with this, let me know and I'll add it.


Solution

  • GoogleApiClient can be attached to an Activity, and be set to automanage, or it can be attached to the application (or another Singleton), and be manually managed. Your attaching it to a non-ui Fragment, makes no sense, as it has no lifecycle and cannot be auto-managed.
    For your needs, extend the application class (Google for instructions), add a GoogleApiClient variable to it. Initialize it in onCreate of the application, and handle the onConnected callback.