Reputation: 360
I am trying to create a method that authentificates a Google user on Firebase.
I am using this method in 3 different activities, and I would like to clean up the code making a method.
This is the code im trying to make as a static method in a class.
private void firebaseAuthWithGoogle(GoogleSignInAccount acct, final Context activity, FirebaseAuth firebaseAuth) {
Log.d("MainACtivity", "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("Main", "signInWithCredential:onComplete:" + task.isSuccessful());
if (!task.isSuccessful()) {
Log.w("MainAcitivyt", "signInWithCredential", task.getException());
Toast.makeText(activity, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
else{
startActivity(AccountCreationActivity.newIntent(activity));
}
}
});
}
I am having problems to understand how I can use the 'startActivity' from another class when called on my methods. Or second choice I also have been trying is to return a intent, but since 'onComplete' returns void im totally lost how to proceed.
Here is the code for my Google sign-up/login that I would like to copy in different activities and was planning to create a method of it in another class. I think the only method I cant take out from my sign-up Activity is the 'onACtivityResult'
private void signIn() {
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);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
// ...
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d("MainACtivity", "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("Main", "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w("MainAcitivyt", "signInWithCredential", task.getException());
Toast.makeText(getActivity(), "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
else{
startActivity(AccountCreationActivity.newIntent(getActivity()));
}
}
});
}
Upvotes: 0
Views: 196
Reputation: 2309
how I can use the 'startActivity' from another class
Use instance of Context
to start activity. You have Context activity
in method parameters, use it.
private void firebaseAuthWithGoogle(GoogleSignInAccount acct, final Context activity, FirebaseAuth firebaseAuth) {
Log.d("MainACtivity", "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("Main", "signInWithCredential:onComplete:" + task.isSuccessful());
if (!task.isSuccessful()) {
Log.w("MainAcitivyt", "signInWithCredential", task.getException());
Toast.makeText(activity, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
else{
// This line has been changed.
activity.startActivity(AccountCreationActivity.newIntent(activity));
}
}
});
}
Upvotes: 1