问题
I tried to integrate an AlertDialog into my app via which the customer can log out. When I call up the dialog, it just works as it should be. However, if I press "log out" Iam not forwarded to the LoginActivity as requested. Instead, my dialog simply closes and the HomeActivity is reloaded.
What am I doing wrong here? Greetings and thanks!
private void signOut() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Signout")
.setMessage("Do you really want to sign out?")
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Common.selectedFood = null;
Common.categorySelected = null;
Common.currentUser = null;
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(HomeActivity.this, LoginActivity.class));
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.BLACK);
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.BLACK);
}
LoginActivity
public class LoginActivity extends AppCompatActivity {
View view;
private EditText Email;
private EditText Password;
private Button Anmelden;
private TextView BenutzerRegistrierung;
private TextView Spaeter;
private FirebaseAuth firebaseAuth;
private ProgressDialog progressDialog;
private TextView Passwortzurücksetzen;
private CheckBox chkBoxRememberMe;
String email, password;
@Override
protected void onCreate(Bundle savedInstanceState) {
view = this.getWindow().getDecorView();
view.setBackgroundResource(R.color.colorBlack);
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
Email = (EditText) findViewById(R.id.editTextEmail);
Password = (EditText) findViewById(R.id.editTextPasswort);
Anmelden = (Button) findViewById(R.id.buttonLogin);
Passwortzurücksetzen = (TextView) findViewById(R.id.textViewPasswortzurück);
BenutzerRegistrierung = (TextView) findViewById(R.id.textViewRegistrierung);
Spaeter = (TextView) findViewById(R.id.textViewSpaeter);
chkBoxRememberMe=(CheckBox)findViewById(R.id.checkBox);
FirebaseApp.initializeApp(this);
firebaseAuth = FirebaseAuth.getInstance();
progressDialog = new ProgressDialog(this);
SharedPreferences preferences=getSharedPreferences("checkbox",MODE_PRIVATE);
String checkbox = preferences.getString("remember","");
if(checkbox.equals("true")){
Intent intent =new Intent(LoginActivity.this,HomeActivity.class);
startActivity(intent);
}else if(checkbox.equals("true")){
Toast.makeText(this,"Bitte anmleden",Toast.LENGTH_SHORT).show();
}
if (firebaseAuth.getCurrentUser()!=null) {
finish();
startActivity(new Intent(LoginActivity.this, PasswordActivity.class));
}
Anmelden.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
password = Password.getText().toString();
email = Email.getText().toString();
if (email.isEmpty() || password.isEmpty()) {
Toast.makeText(LoginActivity.this, "Bitte geben Sie ihre Anmeldedaten ein", Toast.LENGTH_LONG).show();
} else {
prüfen(Email.getText().toString(), Password.getText().toString());
}
}
});
BenutzerRegistrierung.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(LoginActivity.this, RegistrationActivity.class));
}
});
Passwortzurücksetzen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(LoginActivity.this, PasswordActivity.class));
}
});
Spaeter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(LoginActivity.this, HomeActivity.class));
}
});
chkBoxRememberMe.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(compoundButton.isChecked()){
SharedPreferences preferences=getSharedPreferences("checkbox",MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("remember","true");
editor.apply();
Toast.makeText(LoginActivity.this, "Checked",Toast.LENGTH_SHORT).show();
}else if (!compoundButton.isChecked()){
SharedPreferences preferences=getSharedPreferences("checkbox",MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("remember","false");
editor.apply();
Toast.makeText(LoginActivity.this, "Unchecked",Toast.LENGTH_SHORT).show();
}
}
});
}
private void prüfen(String userEmail, String userPassword) {
progressDialog.setMessage("Nachricht Ladezeit");
progressDialog.show();
firebaseAuth.signInWithEmailAndPassword(userEmail, userPassword).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
progressDialog.dismiss();
EmailVerifikation();
} else {
Toast.makeText(LoginActivity.this, "Anmeldung Fehlgeschlagen", Toast.LENGTH_LONG).show();
}
}
});
}
private void EmailVerifikation() {
FirebaseUser firebaseUser = firebaseAuth.getInstance().getCurrentUser();
Boolean emailflag = firebaseUser.isEmailVerified();
if (emailflag) {
if(firebaseAuth.getCurrentUser()!=null) {
final FirebaseDatabase database =FirebaseDatabase.getInstance();
DatabaseReference myref=database.getReference("Users").child(firebaseAuth.getUid());
myref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
UserModel userModel= dataSnapshot.getValue(UserModel.class);
currentUser=userModel;
startActivity(new Intent(LoginActivity.this, HomeActivity.class));
finish();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(LoginActivity.this, ""+databaseError.getCode(), Toast.LENGTH_SHORT).show();
}
});
}
} else {
Toast.makeText(this, "Verifiziere bitte zuerst deine Email-Adresse", Toast.LENGTH_SHORT).show();
firebaseAuth.signOut();
}
}
}
回答1:
As I said in the comments.
You are starting the LoginActivity
before the Firebase actually logout the user. Try to add a FirebaseAuth.AuthStateListener and just start the LoginActivity
when the listener is trigged.
回答2:
I added the following code to my LoginActivity OnCreate;
listener = firebaseAuth -> {
Dexter.withActivity(this)
.withPermissions(
Arrays.asList(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA)
)
.withListener(new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport report) {
if(report.areAllPermissionsGranted())
{
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
} else {
}
}
else
Toast.makeText(LoginActivity.this, "You must accept all permissions", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
}
}).check();
};
来源:https://stackoverflow.com/questions/61664357/firebase-signout-is-not-leading-to-the-correct-activity