I'm a new android developer and a student. I'm having difficulty of if the user is already login in my app. I will show some of my codes. I greatly appreciate for the help and answer.
Login.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if(opr.isDone()) {
Log.d(TAG,"Got cached sign-in");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
SignInButton mGoogleSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
mStatusView = (TextView) findViewById(R.id.status_text);
mGoogleSignInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
signInWithGoogle();
}
}
});
}
}
private void signInWithGoogle(){
if(mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Auth.GOOGLE_SIGN_IN_API,gso).build();
final Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode,data);
if(requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if(result.isSuccess()) {
GoogleSignInAccount account = result.getSignInAccount();
mStatusView.setText(getString(R.string.signed_in_fmt, account.getDisplayName()));
//TODO: Start another explicit intent here. but this time, start MainActivity
Intent intent = new Intent(Login.this , MainActivity.class);
startActivity(intent);
finish();
} else {
}
}
where did i go wrong ?
For Google Login check whether user is signed in or not:
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
// User is signed in.
// ...
} else {
// User is not signed in.
// Perform your operation.
}
GoogleApiClient mGoogleApiClient is deprecated now! So according doc
// Check for existing Google Sign In account, if the user is already signed in
// the GoogleSignInAccount will be non-null.
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
updateUI(account);
For example:
if (GoogleSignIn.getLastSignedInAccount(context) == null) {
Log.d(TAG_SIGN_FLOW, "User is not signed in, so make login in")
val signInOptions: GoogleSignInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestScopes(Scope(DriveScopes.DRIVE_FILE))
.build()
startActivityForResult(GoogleSignIn.getClient(this.requireActivity(), signInOptions).signInIntent, REQUEST_CODE_SIGN_IN)
} else {
Log.d(TAG_SIGN_FLOW, "User has already been login")
}
Assuming that you are checking authentication status from any Activity for eg Main Activity (Incase fragment get a singleton instance of your context from any activity which it is triggered from).
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
if (mGoogleApiClient.isConnected()) {
//Todo
}
}
I know it has been 6 years already but I can try to help. To check whether user is logged in and immediately open the MainActivity override the on Start method
@Override
protected void onStart() {
super.onStart()
FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseUser fUser = auth.getCurrentUser();
if (fUser != null){
startActivity(new Intent(LoginActivity.this, MainActivity.class));
}
}
The above code will do the trick. Happy coding:)
Made change in below code-
Maintain a boolean variable in sharedPreferance and will work for you.
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
sharedPreferences =getSharedPreferences(getPackageName(), MODE_PRIVATE);
editor = sharedPreferences.edit();
if(sharedPreferences.getBoolean("isLogin",false))
{
Intent intent = new Intent(Login.this , MainActivity.class);
startActivity(intent);
finish();
}
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if(opr.isDone()) {
Log.d(TAG,"Got cached sign-in");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
SignInButton mGoogleSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
mStatusView = (TextView) findViewById(R.id.status_text);
mGoogleSignInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
signInWithGoogle();
}
}
});
}
}
private void signInWithGoogle(){
if(mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Auth.GOOGLE_SIGN_IN_API,gso).build();
final Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode,data);
if(requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if(result.isSuccess()) {
GoogleSignInAccount account = result.getSignInAccount();
mStatusView.setText(getString(R.string.signed_in_fmt, account.getDisplayName()));
//TODO: Start another explicit intent here. but this time, start MainActivity
sharedPreferences.edit().putBoolean("isLogin",true).apply();
Intent intent = new Intent(Login.this , MainActivity.class);
startActivity(intent);
finish();
} else {
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.