I had posted this question 2 days ago. onActivityResult method for both google+ and facebook
And I thought that maybe I can create two request codes, one for facebook and one for google + and then in onActivityResult method, I can create a switch case or else if statement using the request codes. But i don't know how to do that exactly. How can I get signIn intent for facebook. Can someone help me with that line of code, if what I am thinking can actually work ...
public abstract class google_abstract extends Activity {
CallbackManager callbackManager;
FirebaseAuth auth;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
// Build a GoogleSignInClient with the options specified by gso.
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("Success", "Login");
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "Login Cancel", Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException exception) {
Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
int RC_SIGN_IN_G = 0;
int RC_SIGN_IN_F = 0;
GoogleSignInClient mGoogleSignInClient;
public void signIn_g() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN_G);
}
**public void signIn_f(){
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN_F);
}**
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN_G) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
else if (requestCode == RC_SIGN_IN_F) {
callbackManager.onActivityResult(requestCode,resultCode,data );
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "public_profile"));
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
startActivity(new Intent(this, tags.class));
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w("Google Sign In Error", "signInResult:failed code=" + e.getStatusCode());
// pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onStart() {
// 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);
if (account != null) {
startActivity(new Intent(this, tags.class));
}
super.onStart();
}
private void handleFacebookToken(AccessToken accessToken)
{
AuthCredential credential = FacebookAuthProvider.getCredential(accessToken.getToken());
auth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
FirebaseUser myuserobj=auth.getCurrentUser();
updateUI(myuserobj);
}
else
{
Toast.makeText(getApplicationContext(),"could not register to firebase",Toast.LENGTH_SHORT).show();
}
}
});
}
private void updateUI(FirebaseUser myuserobj) {
String e_mail= myuserobj.getEmail();
}
}
Currently you are storing the same int (here 0) so the onActivityResult will always go in the first if statement. Solution - Firstly you need to take integer values globally like this
For google you need to have request code and then fetch it in onActivityResult and check and for fb include this line before the super call
and remove fb request code from onActivityResult
to call fb login you need to find the fb login button by its id and attach the callback listener
Final Code of onActivityResult is here
EDIT FB LOGIN CODE ADDED
for google leave it the same for fb use this
fb login code