I am facing the issue in creating the google api connection in Preference Fragment ,I created GoogleApiClient connection in an activity but in fragment is connectiong. When the fragment is appeared the connection failed is shown.
The code for GoogleApiClient is
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
The above code is placed inside the onCreateView() method in a fragment.
The connection call backs are
@Override
public void onConnected(@Nullable Bundle bundle) {
Toast.makeText(getActivity(),"Connected Successfully",Toast.LENGTH_SHORT).show();
Log.e("SUCCESSFULL", "Connection successful");
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
if(connectionResult.hasResolution())
{
try {
connectionResult.startResolutionForResult(getActivity(),RESOLVE_CONNECTION_REQUEST_CODE);
Toast.makeText(getActivity(),"Connected Failed",Toast.LENGTH_SHORT).show();
Log.e("SUCCESSFULL", "Connection Failed.....");
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
else {
GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(),getActivity(),0);
}
}
How to solve this issue and make the google api client connection in fragment.