Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Api Client sometime NULL in onConnected

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.

like image 289
quangson91 Avatar asked Apr 14 '15 04:04

quangson91


Video Answer


4 Answers

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.

like image 160
Ben Faingold Avatar answered Sep 22 '22 03:09

Ben Faingold


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);
}
like image 32
abedfar Avatar answered Sep 18 '22 03:09

abedfar


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.

like image 35
Yoon-Geun Kwon Avatar answered Sep 18 '22 03:09

Yoon-Geun Kwon


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

like image 34
Aditya Verma Avatar answered Sep 21 '22 03:09

Aditya Verma