Search code examples
androidandroid-studiogoogle-authentication

Signing out google account from fragement(bottomnavigation)


Since the sign out button is on fragment, I cant use "this" as Activity, anything I can replace with?

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(getString(R.string.default_web_client_id))
        .requestEmail()
        .build();
GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this,gso);

error message

error: no suitable method found for getClient(DashboardFragment,GoogleSignInOptions)
GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this,gso);
                                                     ^
method GoogleSignIn.getClient(Context,GoogleSignInOptions) is not applicable
  (argument mismatch; DashboardFragment cannot be converted to Context)
method GoogleSignIn.getClient(Activity,GoogleSignInOptions) is not applicable
  (argument mismatch; DashboardFragment cannot be converted to Activity)

Solution

  • GoogleSignIn.getClient(Context,GoogleSignInOptions) requires a Context rather than a fragment.

    Use it this way:

    GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(getContext(),gso);
    

    Be aware that getContext() may return null if the fragment is not attached.