18

I implement GoogleApiClient as bellow:

 mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this, 0 /* clientId */, this)
                .addApi(LocationServices.API)
                .addApi(Places.GEO_DATA_API)
                .addConnectionCallbacks(this)
                .build();

But in onConnected method I check mGoogleApiClient => value null. In this case I try to re-build googleApiClient but I get error:

  java.lang.IllegalStateException: Already managing a GoogleApiClient with id 0

Please help me understand why mGoogleApiClient is sometimes NULL althought it's connected :|. (Notes. I checked all source code, I never set GoogleApiClient to NULL).

Thanks!

Update

My problem now solved after I try use latest version of play-service.

Thanks everybody for help.

Artem Mostyaev
  • 3,874
  • 10
  • 53
  • 60
quangson91
  • 1,044
  • 1
  • 16
  • 30
  • I have the same issue but i am using google play servcies, 8.4.0 latest version, but this issue still happening – neena Apr 27 '16 at 15:43

6 Answers6

36

I had the same problem. All I did to solve it is remove .enableAutoManage(this, 0 /* clientId */, this) because it just doesn't work properly from what I assumed. Then, override these methods in your activity:

@Override
public void onStart() {
    super.onStart();
    if (mGoogleApiClient != null) {
        mGoogleApiClient.connect();
    }
}

@Override
public void onStop() {
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
    super.onStop();
}

Technically, that is what .enableAutoManage(this, 0 /* clientId */, this) was supposed to do, except that now, everything works as it should.

Ben Faingold
  • 563
  • 5
  • 12
13

Documentation says: At any given time, only one auto-managed client is allowed per id. To reuse an id you must first call stopAutoManage(FragmentActivity) on the previous client.

What I personally do is making a call to bellow method before I leave the activity, in which I am using the Google Api Client.

private void stopAutoManage() {
    if (mGoogleApiClient != null)
        mGoogleApiClient.stopAutoManage(mActivity);
}
abedfar
  • 1,989
  • 24
  • 21
  • Still get same error, can you check [this question](https://stackoverflow.com/questions/56037170/google-api-client-already-managing-with-id-0) please @abedfar – ysfcyln May 08 '19 at 12:03
6

I think you'd better watch this reference.

reference page of "public GoogleApiClient.Builder enableAutoManage"

In this page shows that, through IllegalStateException if clientId is already being auto-managed. So, check on your code with

                .enableAutoManage(this, 0 /* clientId */, this)

I think if exception on your code, it could return zero as not completed.

  • It's only happen if I rebuild Google API. But current I build it complete. method "onConnected" called. But mGooogleApiClient is NULL :| I debug & check it many time. (it's not always happend. Feq about 1/10). – quangson91 Apr 14 '15 at 05:01
  • I see why error " java.lang.IllegalStateException: Already managing a GoogleApiClient with id 0" happend. I care about why mGoogleApiClient have value NULL in onConnected method :|, please help. Thanks – quangson91 Apr 14 '15 at 05:02
2

If u are facing this problem when u try to reinitialize mGoogleApiClient, then just remove

.enableAutoManage(this, 0 /* clientId */, this)

Use

mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addApi(Places.GEO_DATA_API)
            .addConnectionCallbacks(this)
            .build();

and it will work fine

Aditya Verma
  • 91
  • 1
  • 10
1

I had the same problem (Already managing a GoogleApiClient with id 0) in a fragment, and finally I resolved it :

  • Override onStart() and onStop() normally
  • Add in onStop() call yourApiGoogle.stopAutoManage(context);

Have a nice day...

ppreetikaa
  • 1,149
  • 2
  • 15
  • 22
ach2ateiem
  • 11
  • 2
0

build() calls onConnected immediately if you are already connected. Therefore, your variable might be null.

Better use

mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, 0 /* clientId */, this)
            .addApi(LocationServices.API)
            .addApi(Places.GEO_DATA_API)
            .addConnectionCallbacks(this);
mGoogleApiClient.build();
friedger
  • 645
  • 7
  • 21