using enableAutoManage() in fragment

后端 未结 4 361
说谎
说谎 2020-11-29 05:04

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 =         


        
相关标签:
4条回答
  • 2020-11-29 05:44

    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();
    }
    
    0 讨论(0)
  • 2020-11-29 05:46

    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();
    
    0 讨论(0)
  • 2020-11-29 05:47

    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();
    
    0 讨论(0)
  • 2020-11-29 05:59

    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();
    }
    
    0 讨论(0)
提交回复
热议问题