25

Is there another way to connect Google API client?

I use auto complete places and I have to use this code some where in MYFRAGMENT

mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
                .addApi(Places.GEO_DATA_API)
                .enableAutoManage(this, GOOGLE_API_CLIENT_ID, this)
                .addConnectionCallbacks(this).build();

My problem with

enableAutoManage(this, GOOGLE_API_CLIENT_ID, this)
                    .addConnectionCallbacks(this).build();

I can't deal with it because when I replace this with getActivity() I have many problem with casting

thanks for help and sorry if this question is silly.

Torsten Knodt
  • 477
  • 1
  • 5
  • 20
Hamza Dabjan
  • 393
  • 1
  • 5
  • 11

4 Answers4

62

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();
}
Torsten Knodt
  • 477
  • 1
  • 5
  • 20
Matthew Pape
  • 1,681
  • 1
  • 22
  • 25
  • 6
    Is it really just connect/disconnect? By looking at GoogleApiClient sources, it seems to do something more than that. I cannot tell for sure, because it's hard to read their obfuscated code. I just don't want to extend from `FragmentActivity`, because my app does not support old API levels anyway. Extending from `FragmentActivity` also brings more issues (with animations, different FragmentManager and LoaderManager.LoaderCallbacks), so I'd better just stay at `Activity`. But I want to mimic exact behavior of `enableAutoManage()`. – Dzmitry Lazerka Dec 18 '15 at 07:04
  • The docs for [GoogleApiClient](https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient) suggest that in addition to the start and stop behavior, it attempts to handle some connection errors also. If you don't enableAutoManager then you have to add this logic yourself in the `GoogleApiClient.ConnectionCallbacks` you add to your client. It's hard to say what specific actions they take in these cases. – Matthew Pape Jan 05 '16 at 22:11
  • And the thing is: the example code provided by Google is using `AppCompatActivity` and not a Fragment but still using then `enableAutoManage`. How can this be? – Moritz Mar 21 '16 at 09:59
  • 1
    I am not sure which example code you are referring to. Did you mean to say they use an `AppCompatActivity` instead of a `FragmentActivity`? This is possible because `AppCompatActivity` extends `FragmentActivity`. – Matthew Pape Mar 21 '16 at 19:38
  • Stupidiest tight coupling ever. Is Google really serious about this api client class? – eC Droid Sep 06 '17 at 10:54
  • 3
    I would definitely recommend checking out the new GeoDataClient class instead. It lets you skip the hassle of dealing with the GoogleApiClient class. (https://developers.google.com/android/reference/com/google/android/gms/location/places/GeoDataClient) – Matthew Pape Sep 06 '17 at 16:38
2

Sorry for late reply but rather than extending FragmentActivity you can extend AppCompatActivity...

public class YourActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener 

.....

mCredentialsApiClient = new GoogleApiClient.Builder(context)
                    .addConnectionCallbacks(this)
                    .enableAutoManage(this,this)
                    .addApi(Auth.CREDENTIALS_API)
                    .build();
Shivam Mathur
  • 111
  • 1
  • 3
1

If your fragment is running in a FragmentActivity, or AppCompatActivity you can do something like this:

        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
            .enableAutoManage((FragmentActivity) getActivity() /* FragmentActivity */, new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                    // your code here
                }
            })
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
Reid
  • 4,376
  • 11
  • 43
  • 75
0

My solution is similar to accepted answer except, I use second signature of Builder so that connectionFailedListener is also send to the constructor.

Followed by mGoogleApiClient.connect() and mGoogleApiClient.disconnect() in onStart() and onStop() respectively

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mGoogleApiClient = new GoogleApiClient.Builder(this /*context*/ , this /*connectedListener*/, this /**connectionFailedListener/)
            .addApi(Places.GEO_DATA_API)
            .build();
}
Asad Ali Choudhry
  • 4,985
  • 4
  • 31
  • 36
Ajitsen
  • 338
  • 3
  • 7