2

basically, I am creating an app where a user will be able to sign in to their Google Accounts. But I am facing a problem and it is discussed below with codes. I have a MainActivity, where I have created a GoogleApiClient to sign in with Google, Now after signing out, if I again press sign in, then it is signing in with the same account. I know it is because I am not signing out from the google account but only from the firebase account. here is my MainActivity

public class MainActivity extends AppCompatActivity {

private SignInButton mGoogleButton;
private static final int RC_SIGN_IN=1;
private GoogleApiClient mGoogleApiClient;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private static final String TAG = "Main Activity";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mAuth = FirebaseAuth.getInstance();
    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

            if (firebaseAuth.getCurrentUser() != null)
            {
                startActivity(new Intent(MainActivity.this,GPS.class));
                finish();
            }
        }
    };
    mGoogleButton = (SignInButton) findViewById(R.id.googleButton);



    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
            .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                    Toast.makeText(MainActivity.this,"Error Signing",Toast.LENGTH_SHORT)
                            .show();
                }
            }).addApi(Auth.GOOGLE_SIGN_IN_API,gso)
            .build();

    mGoogleButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            signIn();
        }
    });
}

@Override
protected void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);

}

private void signIn() {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

Now I have another Activity, in which I want to get the GoogleApiClient of the MainActivity, how to get the same GoogleApiClient in the second activity? I have signout() but only from Firebase Account. Here is my second activity.

public class GPS extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gps);
    mToolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(mToolbar);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);

    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
    navigationView.setNavigationItemSelectedListener(this);
    mAuth = FirebaseAuth.getInstance();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(mToggle.onOptionsItemSelected(item)){
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
   int id=item.getItemId();

        if(id == R.id.nav_account) {
            Toast.makeText(this,"Account Edit Here",Toast.LENGTH_SHORT).show();
        }
       if (id == R.id.settings) {
            Toast.makeText(this,"Settings",Toast.LENGTH_SHORT).show();
        }
        if (id == R.id.logout) {
            mAuth.signOut();
            startActivity(new Intent(GPS.this,MainActivity.class));
            finish();
        }
            //close navigation drawer
    mDrawerLayout.closeDrawer(GravityCompat.START);
    return true;
}

I have searched everything and found the same question, but following the answer is quite difficult there. Here is what I found passing the GoogleApiClient obj from one activity to another

Mahesh Vayak
  • 1,056
  • 12
  • 25
Bikram Nath
  • 483
  • 6
  • 15

0 Answers0