我正在尝试向我的应用添加注销/登出功能。我正在YouTube上关注此video来做到这一点。注销/注销功能正常工作,但是在登录屏幕中注销后,我可以返回到应用程序。我仍然没有尝试使用onBackPressed()
方法。也许我可以努力防止后退操作,但是我很好奇为什么我的应用无法正常工作。
这是登出片段代码
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);
}
}
}
答案 0 :(得分:1)
我建议像下面这样更改标志的顺序。
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
答案 1 :(得分:0)
将Intent标志设置为如下所示
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // To clean up all activities
希望这对您有用。