问题描述
我试图将AlertDialog集成到我的应用程序中,客户可以通过该对话框注销.当我调出对话框时,它应该可以正常工作.但是,如果我按注销",则不会按要求将Iam转发到LoginActivity.相反,我的对话框只是关闭,然后重新加载HomeActivity.
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();
}
}
}
推荐答案
正如我在.
在Firebase实际注销用户之前,您正在启动 LoginActivity
.尝试添加 FirebaseAuth.AuthStateListener ,并在触发侦听器时启动 LoginActivity
.
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.
这篇关于Firebase注销未导致正确的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!