using enableAutoManage() in fragment
Asked Answered
M

4

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.

Maternity answered 3/6, 2015 at 14:24 Comment(4)
Which "this" are you replacing? Just the first one? Is your activity a FragmentActivity?Nobleminded
first "this" in enableAutoManage() I have an error and I fix it by catsing to FragmentActivity but when run the app it stoppedMaternity
No my MainActivity is not FragmentActivityMaternity
@Hamza can you please tell me the type of GOOGLE_API_CLIENT_ID. as enableAutoManage accept int but our google api key is string.Unclog
N
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();
}
Nobleminded answered 3/6, 2015 at 18:0 Comment(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().Schuller
The docs for 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.Nobleminded
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?Erlin
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.Nobleminded
Stupidiest tight coupling ever. Is Google really serious about this api client class?Busybody
I would definitely recommend checking out the new GeoDataClient class instead. It lets you skip the hassle of dealing with the GoogleApiClient class. (developers.google.com/android/reference/com/google/android/gms/…)Nobleminded
H
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();
Holystone answered 1/12, 2017 at 5:40 Comment(0)
S
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();
Sophist answered 31/1, 2017 at 21:41 Comment(0)
A
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();
}
Adina answered 26/4, 2019 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.