I've started using Dagger2 to manage dependencies and I'm trying to understand how I can use DI to provide a singleton GoogleApiClient. The motivations for this are:
I want to provide a Singleton GoogleApiClient at the Application scope.
How do you handle callbacks? Whether you choose an auto-managed or manually-managed connection, there are some callbacks that must be handled:
You can use the injection to create the client
@Provides
@Singleton
GoogleApiClient providesGoogleApiClient(Context context) {
return new GoogleApiClient.Builder(context)
.addApi(Places.GEO_DATA_API)
.addApi(LocationServices.API)
.build();
}
And then manage the call backs on your activity
@Inject GoogleApiClient mGoogleApiClient;
if (mGoogleApiClient != null) { mGoogleApiClient.registerConnectionCallbacks(this); mGoogleApiClient.registerConnectionFailedListener`(this);
}
I hope this might help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With