基本上,我正在创建一个用户可以登录其Google帐户的应用。但是我遇到了一个问题,下面将用代码进行讨论。 我有一个MainActivity,我已经创建了一个GoogleApiClient来登录Google,现在退出后,如果我再次按下登录,那么它将使用相同的帐户登录。我知道这是因为我没有从Google帐户退出,只能从firebase帐户退出。这是我的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);
}
现在我有另一个Activity,其中我想获得MainActivity的GoogleApiClient,如何在第二个活动中获得相同的GoogleApiClient?我有退出()但仅限Firebase帐户。这是我的第二项活动。
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;
}
我搜索了所有内容并发现了同样的问题,但是在那里回答是非常困难的。以下是我发现的passing the GoogleApiClient obj from one activity to another