I am trying to add a logout/signout function to my app. I am following this video on youtube to do that. Logout/signout function is working just fine but after log out in the login screen I can go back to the app. I am still not trying using the onBackPressed() method. Maybe I can work to prevent the back action, but I am just curious why my app not working.
Here's the sign out fragment code
public class SignOutFragment extends Fragment {
private static final String TAG = "SignOutFragment";
//firebase
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private ProgressBar mProgressBar;
private TextView tvSignout, tvSigningOut;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_signout, container, false);
tvSignout = (TextView) view.findViewById(R.id.tvConfirmSignout);
mProgressBar = (ProgressBar) view.findViewById(R.id.progressBar);
tvSigningOut = (TextView) view.findViewById(R.id.tvSigningOut);
Button btnConfirmSignout = (Button) view.findViewById(R.id.btnConfirmSignout);
mProgressBar.setVisibility(View.GONE);
tvSigningOut.setVisibility(View.GONE);
setupFirebaseAuth();
btnConfirmSignout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: attempting to sign out.");
mProgressBar.setVisibility(View.VISIBLE);
tvSigningOut.setVisibility(View.VISIBLE);
mAuth.signOut();
}
});
return view;
}
/*
------------------------------------ Firebase ---------------------------------------------
*/
private void setupFirebaseAuth(){
Log.d(TAG, "setupFirebaseAuth: setting up firebase auth.");
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
Log.d(TAG, "onAuthStateChanged: navigating back to login screen.");
//Intent intent = new Intent(getActivity(), LoginActivity.class);
Intent intent = new Intent(getContext(),LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
getActivity().finish();
}
// ...
}
};
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
}