title: Facebook and Google Plus integration in android app
body: <p>I'm trying to integrate facebook and google plus login in my android app and they individually works pretty well.</p>
  
  <p>When I try to integrate both of them at a time they dosent seem to work fine.</p>
  
  <p>This is my structure flow:</p>
  
  <p>1.LoginActivity---->Users clicks either google+ or facebook login button.</p>
  
  <ol start="2">
  <li>If the user authenticates then he will be moved to my MainActivity.</li>
  </ol>
  
  <p>This is my LoginActivity:</p>
  
  <pre><code>public class GoogleLogin extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
  private static final int RC_SIGN_IN = 0;
  
  private GoogleApiClient mGoogleApiClient;
  
  private boolean mIntentInProgress;
  
  private boolean mSignInClicked;
  
  private ConnectionResult mConnectionResult;
  
  public static GoogleLogin mContext;
  
  private UiLifecycleHelper uiHelper;
  
  Button fbutton;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  mContext = this;
  
  uiHelper = new UiLifecycleHelper(this, statusCallback);
  uiHelper.onCreate(savedInstanceState);
  
  
  setContentView(R.layout.login);
  mGoogleApiClient = new GoogleApiClient.Builder(this)
  .addConnectionCallbacks(this)
  .addOnConnectionFailedListener(this)
  .addApi(Plus.API, PlusOptions.builder().build())
  .addScope(Plus.SCOPE_PLUS_LOGIN)
  .build();
  
  
  findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  // TODO Auto-generated method stub
  if (v.getId() == R.id.sign_in_button
  &amp;&amp; !mGoogleApiClient.isConnecting()) {
  mSignInClicked = true;
  resolveSignInError();
  }
  }
  });
  
  
  
  fbutton = (Button) findViewById(R.id.btnflogin);
  
  findViewById(R.id.btnflogin).setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View arg0) {
  
      // start Facebook Login
      Session.openActiveSession(GoogleLogin.this, true, new Session.StatusCallback() {
  
        // callback when session changes state
        @Override
        public void call(Session session, SessionState state, Exception exception) {
          if (session.isOpened()) {
  
            // make request to the /me API
            Request.newMeRequest(session, new Request.GraphUserCallback() {
  
              // callback after Graph API response with user object
              @Override
              public void onCompleted(GraphUser user, Response response) {
                if (user != null) {
  
  
                    Intent i = new Intent(GoogleLogin.this,MainActivity.class);
  
  
                      //Add your data from getFactualResults method to bundle
                      i.putExtra("Facebook", "Logged in using Facebook Account");
                      i.putExtra("LastName", user.getLastName());
                      i.putExtra("FirstName", user.getFirstName());
                      //Add the bundle to the intent
                      i.putExtras(i);
                      startActivityForResult(i, 1);
                 }
              }
            }).executeAsync();
          }
        }
      });
  
  
  }
  });
  
  }
  
  
  private Session.StatusCallback statusCallback = new Session.StatusCallback() {
      @Override
      public void call(Session session, SessionState state,
              Exception exception) {
          if (state.isOpened()) {
  
              Log.d("FacebookSampleActivity", "Facebook session opened");
          } else if (state.isClosed()) {
  
              Log.d("FacebookSampleActivity", "Facebook session closed");
          }
      }
  };
  
  @Override
  public void onResume() {
      super.onResume();
      uiHelper.onResume();
  }
  
  @Override
  public void onPause() {
      super.onPause();
      uiHelper.onPause();
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
      uiHelper.onDestroy();
  }
  
  
  @Override
  public void onSaveInstanceState(Bundle savedState) {
      super.onSaveInstanceState(savedState);
      uiHelper.onSaveInstanceState(savedState);
  }
  
  
  
  private void resolveSignInError() {
  if (mConnectionResult.hasResolution()) {
  try {
  mIntentInProgress = true;
  startIntentSenderForResult(mConnectionResult.getResolution().getIntentSender(),
  RC_SIGN_IN, null, 0, 0, 0);
  } catch (SendIntentException e) {
  // The intent was canceled before it was sent. Return to the default
  // state and attempt to connect to get an updated ConnectionResult.
  mIntentInProgress = false;
  mGoogleApiClient.connect();
  }
  }
  }
  
  protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
      uiHelper.onActivityResult(requestCode, responseCode, intent);
  
  if (requestCode == RC_SIGN_IN) {
  if (responseCode != RESULT_OK) {
  mSignInClicked = false;
  }
  
  mIntentInProgress = false;
  
  if (!mGoogleApiClient.isConnecting()) {
  mGoogleApiClient.connect();
  }
  }
  
  }
  
  protected void onStart() {
  super.onStart();
  mGoogleApiClient.connect();
  }
  
  
  protected void onStop() {
  super.onStop();
  if (mGoogleApiClient.isConnected()) {
  mGoogleApiClient.disconnect();
  }
  }
  
  public void onConnectionFailed(ConnectionResult result) {
  if (!mIntentInProgress) {
  // Store the ConnectionResult so that we can use it later when the user clicks
  // 'sign-in'.
  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();
  }
  }
  }
  
  public void onConnected(Bundle connectionHint) {
  // We've resolved any connection errors. mGoogleApiClient can be used to
  // access Google APIs on behalf of the user.
  mSignInClicked = false;
  Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
  if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
  
  Intent i = new Intent(getApplicationContext(),MainActivity.class);
  Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
  
  //Create the bundle
  Bundle bundle = new Bundle();
  //Add your data from getFactualResults method to bundle
  bundle.putString("Google", "Logged in using Google Account");
  bundle.putString("GoogleUsername",  currentPerson.getDisplayName());
  //Add the bundle to the intent
  i.putExtras(bundle);
  startActivity(i);
  
  }
  
  }
  
  public void onConnectionSuspended(int cause) {
  
  mGoogleApiClient.connect();
  }
  
  public void gplusSignout(View v){
  //if (v.getId() == R.id.gPlusSignout) {
  if (mGoogleApiClient.isConnected()) {
  Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
  mGoogleApiClient.disconnect();
  //mGoogleApiClient.connect();
  Log.d("Debug","Disconnected");
  Toast.makeText(this, "User is disconnected!", Toast.LENGTH_LONG).show();
  }
  //}
  }
  
  
  }
  </code></pre>
  
  <p>These are bit confusing me :</p>
  
  <pre><code>private void resolveSignInError() {
  if (mConnectionResult.hasResolution()) {
  try {
  mIntentInProgress = true;
  startIntentSenderForResult(mConnectionResult.getResolution().getIntentSender(),
  RC_SIGN_IN, null, 0, 0, 0);
  } catch (SendIntentException e) {
  // The intent was canceled before it was sent. Return to the default
  // state and attempt to connect to get an updated ConnectionResult.
  mIntentInProgress = false;
  mGoogleApiClient.connect();
  }
  }
  }
  
  protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
  
  if (requestCode == RC_SIGN_IN) {
  if (responseCode != RESULT_OK) {
  mSignInClicked = false;
  }
  
  mIntentInProgress = false;
  
  if (!mGoogleApiClient.isConnecting()) {
  mGoogleApiClient.connect();
  }
  }
  
  }
  
  protected void onStart() {
  super.onStart();
  mGoogleApiClient.connect();
  }
  
  
  protected void onStop() {
  super.onStop();
  if (mGoogleApiClient.isConnected()) {
  mGoogleApiClient.disconnect();
  }
  }
  
  public void onConnectionFailed(ConnectionResult result) {
  if (!mIntentInProgress) {
  // Store the ConnectionResult so that we can use it later when the user clicks
  // 'sign-in'.
  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();
  }
  }
  }
  </code></pre>
  
  <p>I'm struck at this point where I'm not sure can I do this way in ActivityResult:</p>
  
  <p>To startup Facebook and move to the next activity this works fine:</p>
  
  <pre><code>@Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
           Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
  
      }
  </code></pre>
  
  <p>But at the same time I have google+ onActivity result..So how do I manage both of them ?</p>
  
  <p>Can anyone say me how do I acheive this in order to get both of them working.</p>