Search code examples
androidgoogle-apilifecycleonactivityresult

Starting an activity after google api client connect


I have a fragment from which the user clicks on a button (from the G+ API) and logs in with the account at his Google Services, so far so good...

The problem is that, when clicking the button, it should start an activity after the login process is finished, BUT it does only (presumably) on destroying the activity (could be on stop, i really dont know) and resuming it again because if i go to menu and go to the app again the second activity starts right away (or when changing orientation, which destroys and recreates the activity).

I know this has something to do with the onActivityResult lifecycle method because my app also has facebook login and i used a uiHelper from the facebook API, i just havent found one for google api client (i dont think it exists actually).

EDIT:

this is my on activity result, if i delete the "uiHelper.onActivityresult(...)" then the same will happen with my facebook login.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    uiHelper.onActivityResult(requestCode, resultCode, data);
}

this is called when the user signs in

@Override
public void onConnected(Bundle bundle) {
    mSignInClicked = false;
    // Get user's information
    getProfileInformation();
}

and this is the getProfileInformation which fills an object i use for my app (is a simple reminders app)

private void getProfileInformation(){
try {
        if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
            RemindersUser.IS_FB_USER =false;
            Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
            String personName = currentPerson.getDisplayName();
            String personPhotoUrl = currentPerson.getImage().getUrl();
            personPhotoUrl = personPhotoUrl.substring(0,personPhotoUrl.length() - 2)+ PROFILE_PIC_SIZE;
            String personGooglePlusId = currentPerson.getId();
            String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
            mRemindersUser =new RemindersUser(
                    personName,
                    email,
                    personPhotoUrl,
                    personGooglePlusId);
            Intent viewPagerIntent = new Intent(getActivity().getApplicationContext(), ViewPagerActivity.class);
            viewPagerIntent.putExtra(RemindersUser.USERNAME, mRemindersUser.getName());
            viewPagerIntent.putExtra(RemindersUser.MAIL, mRemindersUser.getMail());
            viewPagerIntent.putExtra(RemindersUser.IMAGE, mRemindersUser.getImage());
            viewPagerIntent.putExtra(RemindersUser.USER_ID, mRemindersUser.getUserId());
            startActivity(viewPagerIntent);
        } else {
            Toast.makeText(getActivity(),"Person information is null", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

Solution

  • Solved:

    I was managing the life cycle of the Google+ connections wrong, doing it one level of abstraction below than were i should, i started managing the connection from the activity and not from the fragment and everything worked just right.