0

I am having trouble since 2 days ago and I can't find any solution.

I have two buttons, one for signin and another one for signout. If the user is signed in I show the signout button and ıf the user is not signed in then I show the signin button.

The problem starts here; When the user clicks on sign in button I hide the signin button and show the signout button and set its text to "connecting.." if signin flow succeeded I would want to change the text to "signout". If flow fails I hide the signout and show sign in button again. The problem is the text of the signout button remains as "connecting.." even if sign in flow succeeded.

Here is my code: (My class doesn't extends to BaseGameActivity)

mHelper = new GameHelper(this, GameHelper.CLIENT_ALL);
        GameHelper.GameHelperListener listener = new GameHelper.GameHelperListener() {
            @Override
            public void onSignInSucceeded() {
                signin.setVisibility(View.GONE);
                signout.setVisibility(View.VISIBLE);
                signout.setText(R.string.signout);
            }
            @Override
            public void onSignInFailed() {
                signout.setVisibility(View.GONE);
                signin.setVisibility(View.VISIBLE);
            }
        };mHelper.setup(listener);

and the click listener:

 case R.id.sign_in_button:
      signout.setText(R.string.loading);
      signout.setVisibility(View.VISIBLE);
      mHelper.beginUserInitiatedSignIn();
      signin.setVisibility(View.GONE);
      break;
case R.id.sign_out_button:
      mHelper.signOut();
      signin.setVisibility(View.VISIBLE);
      signout.setVisibility(View.GONE);
      break;

Update :

Signin and signout process works, because I can submit score to leaderboard and display it.

epergo
  • 565
  • 5
  • 17
ctarimli
  • 322
  • 4
  • 25

3 Answers3

1

The game samples (https://github.com/playgameservices/android-basic-samples) have been recently updated to not extend BaseGameActivity. I would recommend reviewing them in detail, but here are the high-level items needed to manage the sign-in.

  1. Declare a class member GoogleApiClient mGoogleApiClient and initialize it in the onCreate() method:

    mGoogleApiClient = new GoogleApiClient.Builder(this)    
                    .addConnectionCallbacks(this)  
                    .addOnConnectionFailedListener(this)  
                    .addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN)  
                    .addApi(Games.API).addScope(Games.SCOPE_GAMES)  
                    // add other APIs and scopes here as needed  
                    .build();
    
  2. in the onClick() method for the sign-in button:

    mSignInClicked = true;
    mGoogleApiClient.connect();
    
  3. and for the sign-out button

    Games.signOut(mGoogleApiClient);   
    mGoogleApiClient.disconnect();
    showSignInBar(); // also hides the sign out bar
    
  4. implement the listener interfaces GoogleApiClient.ConnectionCallbacks and GoogleApiClient.OnConnectionFailedListener

  5. implement the listener methods:

    public void onConnected(Bundle connectionHint) {
        showSignOutBar();
        // initialize other data that needs a connection.
    }
    
    public void onConnectionSuspended(int cause) {
        // start the connection process again.
        mGoogleApiClient.connect();
    }
    
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if (this.mResolvingConnectionFailure) {
            Log.d(TAG, "onConnectionFailed() ignoring connection failure; already resolving.");
            return;
        }
        // only try to resolve the connection issue if we actually want to connect
        if (mSignInClicked || mAutoStartSignInFlow) {
            mAutoStartSignInFlow = false;
            mSignInClicked = false;
            mResolvingConnectionFailure = BaseGameUtils
                .resolveConnectionFailure(this, mGoogleApiClient,
                        connectionResult, RC_SIGN_IN,
                        getString(R.string.signin_other_error));
        }
        showSignInBar();
    }
    
  6. handle the sign-in intent in onActivityResult()

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case RC_SIGN_IN:
                mSignInClicked = false;
                mResolvingConnectionFailure = false;
                if (resultCode == RESULT_OK) {
                    mGoogleApiClient.connect();
                } else {
                    BaseGameUtils.showActivityResultError(this, requestCode, resultCode,
                            R.string.signin_failure, R.string.signin_other_error);
                }
                break;
    
        //…..
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    
Clayton Wilkinson
  • 4,524
  • 1
  • 16
  • 25
0

I found this answer Google Play Game Services: strange sign in behavior

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
 super.onActivityResult(requestCode, resultCode, data);
 ....
 <our code here>
}

This answer tell to override the onActivityResult method, by this BaseGameActivity handles the callbacks. But I have to extend my class to BaseGameActivity and use

beginUserInitiatedSignIn();

not:

mHelper.beginUserInitiatedSignIn();

If I don't extend my class to BaseGameActivity when user signs out and want to sign in second time the text of the sign out button doesn't change, remains "connecting..".

Community
  • 1
  • 1
ctarimli
  • 322
  • 4
  • 25
0

Would be a problem if you extend your class from BaseGameActivity? Probably your problems (and others in the future) would be gone if you use that class.

All the necessary methods about Signin in and out and all the Game's API is used extending that class.

I encourage you to use it.

Here you have Google's tutorial for Signin in, hope it helps you:

https://developers.google.com/games/services/training/signin?hl=es

epergo
  • 565
  • 5
  • 17