public class LoginFrag extends DialogFragment {
final int RC_SIGN_IN = 977;
String TAG = "LoginFragLog";
FirebaseAuth mFirebaseAuth;
FirebaseUser mFirebaseUser;
Context mContext;
GoogleSignInClient mGoogleSignInClient;
public LoginFrag() {
// Empty constructor is required for DialogFragment
// Make sure not to add arguments to the constructor
// Use `newInstance` instead as shown below
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.login_frag, container);
if(getDialog() != null){
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
return view;
}
@Override
public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mFirebaseAuth = FirebaseAuth.getInstance();
// Configure Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(mContext, gso);
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
startActivityForResult is crossed and it shows: Call Activity.startActivityForResult(Intent, int) from the fragment's containing Activity.
Deprecated:
use registerForActivityResult(ActivityResultContract, ActivityResultCallback) passing in a androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult object for the ActivityResultContract.
Params:
intent – The intent to start. requestCode – The request code to be returned in onActivityResult(int, int, Intent) when the activity exits. Must be between 0 and 65535 to be considered valid. If given requestCode is greater than 65535, an IllegalArgumentException would be thrown
Similarly, for onActivityResult, it is also crossed out and it shows: Overrides deprecated method in 'androidx.fragment.app.Fragment'
What I am trying to accomplish is: I have Main activity with bottom navigation with fragments and inside one fragment i have a Dialog Fragment which code is this one i.e, LoginFrag. is it because dialog fragment cannot receive call back from login activity of google i.e, onActivityResult. how can i solve this problem, please help. I just want to login from this Dialog Fragment.
@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);
Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId());
firebaseAuthWithGoogle(account.getIdToken());
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
}
}
}
private void firebaseAuthWithGoogle(String idToken) {
AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
mFirebaseAuth.signInWithCredential(credential)
.addOnCompleteListener((Executor) this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mFirebaseAuth.getCurrentUser();
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
}
}
});
}
@Override
public void onAttach(@NotNull Context context) {
super.onAttach(context);
mContext = context;
}
}
Below is the parent Fragment of LoginFrag:
public class DiscussionFragment extends Fragment {
FirebaseAuth mFirebaseAuth;
FirebaseUser user;
public DiscussionFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private void showLogin() {
FragmentActivity fragmentActivity = getActivity();
FragmentManager fm;
assert fragmentActivity != null;
fm = fragmentActivity.getSupportFragmentManager();
LoginFrag loginFrag = new LoginFrag();
loginFrag.setCancelable(false);
loginFrag.show(fm,"login_frag");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.discussion_layout, container, false);
// Inflate the layout for this fragment
mFirebaseAuth = FirebaseAuth.getInstance();
user = mFirebaseAuth.getCurrentUser();
if(user == null){
showLogin();
}
return view;
}
}