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.
If your fragment is running in a FragmentActivity, or AppCompatActivity you can do something like this:
If you want to use
enableAutoManage
then you must make your activity extendFragmentActivity
. The callbacks it makes are required for the automatic management of theGoogleApiClient
to work. So the easiest solution is to addextends 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 youconnect
/disconnect
from the client yourself. The most common place to do this isonStart()
/onStop()
. Something like...Sorry for late reply but rather than extending FragmentActivity you can extend AppCompatActivity...
.....