using enableAutoManage() in fragment

If you want to use enableAutoManage then you must make your activity extend FragmentActivity. The callbacks it makes are required for the automatic management of the GoogleApiClient to work. So the easiest solution is to add extends FragmentActivity to your activity. Then your cast would not fail and cause the app to crash at runtime.

The alternate solution is to manage the api client yourself. You would remove the enableAutoManage line from the builder, and make sure you connect/disconnect from the client yourself. The most common place to do this is onStart()/onStop(). Something like…

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
            .addApi(Places.GEO_DATA_API)
            .addConnectionCallbacks(this).build();
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    mGoogleApiClient.disconnect();
}

Leave a Comment