Android Google subscribes in a loop
I am implementing the google plus sign with Fragment
. I am getting google sign infinite loop in my dialog.
code: Activity:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e("TEST", "requestCode: " + requestCode + ", resultCode: " + resultCode);
if (FragmentLogIn.RC_SIGN_IN == requestCode){
FragmentLogIn fragment = (FragmentLogIn) getSupportFragmentManager().findFragmentById(mainContent.getId());
fragment.onActivityResult(requestCode, resultCode, data);
} else{
super.onActivityResult(requestCode, resultCode, data);
}
}
Fragment:
@Override
public void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
public void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
/**
* Method to resolve any signin errors
* */
private void resolveSignInError() {
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(getActivity(), RC_SIGN_IN);
} catch (SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
@Override
public void onActivityResult(int requestCode, int responseCode, Intent intent)
{
Log.e("GOOGLE+", "requestCode: " + requestCode + "responseCode = " + responseCode);
if (requestCode == RC_SIGN_IN) {
if (responseCode != Activity.RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
}
@Override
public void onConnected(Bundle bundle)
{
Log.e("GOOGLE TEST", "onConnected");
//get user info
try{
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi
.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
String personPhotoUrl = currentPerson.getImage().getUrl();
String personGooglePlusProfile = currentPerson.getUrl();
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
new RetrieveTokenTask().execute(email);
} else {
Log.e("GOOGLE+", "info = null");
}
} catch (Exception e) {
e.printStackTrace();
}
mSignInClicked = false;
}
@Override
public void onConnectionSuspended(int i)
{
Log.e("GOOGLE TEST", "onConnectionSuspended");
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), getActivity(),
0).show();
return;
}
if (!mIntentInProgress) {
// Store the ConnectionResult for later usage
mConnectionResult = result;
if (mSignInClicked) {
// The user has already clicked 'sign-in' so we attempt to
// resolve all
// errors until the user is signed in, or they cancel.
resolveSignInError();
}
}
}
@Override
public void onClick(View view)
{
if (view.getId() == R.id.google_sign_in_button) {
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
} else if (view.getId() == R.id.google_sign_out_button) {
revokeGplusAccess();
}
}
private class RetrieveTokenTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String accountName = params[0];
Log.e("accountName", accountName);
String scopes = "oauth2:profile "+Scopes.PLUS_LOGIN;;
String token = null;
try {
if (getActivity() != null)
token = GoogleAuthUtil.getToken(getActivity().getApplicationContext(), accountName, scopes);
} catch (IOException e) {
Log.e("GOOGLE+", e.getMessage());
} catch (UserRecoverableAuthException e) {
startActivityForResult(e.getIntent(), 1);
} catch (GoogleAuthException e) {
Log.e("GOOGLE+", e.getMessage());
}
return token;
}
@Override
protected void onPostExecute(String s) {
Log.d("TOKEN", "token: " + s);
}
}
private void revokeGplusAccess() {
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status arg0) {
Log.e("GOOGLE+", "User access revoked!");
mGoogleApiClient.connect();
}
});
}
}
This code works when written in Activity
, but when I switch it to Fragment
I get an infinite loop. What am I doing wrong? Does any authority have another example of logging in with Google plus using an access token?
Thanks, Ilan
The endless loop problem occurs when you disconnect googleApiClient
in onStop()
. You should only do this in onDestroy()
.
Hope it helps.
Remove the line
if (mSignInClicked) {
// The user has already clicked 'sign-in' so we attempt to
// resolve all
// errors until the user is signed in, or they cancel.
resolveSignInError();
}
out onConnectionFailed(ConnectionResult result)
, and the endless loop will no longer be.