D
D
Daniel2017-08-23 13:58:08
Google
Daniel, 2017-08-23 13:58:08

How to organize Google Login API to work in different Activities?

Hello.
Faced such a problem that the Google Login API does not work when switching to another Activity.
I created a class that looks like this:

public class GoogleLoginImpl
{
    static String TAG = "GoogleLoginImpl";
    public static GoogleApiClient googleApiClient = null;
    public static GoogleSignInOptions googleSignInOptions = null;
    public static GoogleSignInAccount googleSignInAccount = null;

    public static void init(Context context)
    {
        googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();

        googleApiClient = new GoogleApiClient.Builder(context)
                .enableAutoManage((FragmentActivity)context, new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                        Log.e(TAG, "Connection failed : " + connectionResult.getErrorMessage());
                    }
                })
                .addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
                .build();

        googleApiClient.connect();
    }
}

In MainActivity in the onCreate method I call init () from it and in this Activity there is a login and everything else, but going to another page in which I want to make a profile and a Logout button to disconnect the profile and when checking the connection GoogleLoginImpl.googleApiClient.isConnected() at the output I get false, although in MainActivity I get true.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mr_serg77, 2017-08-23
@mr_serg77

Is there a connection in another activity?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question