У меня есть приложение, которое имеет 3 действия:
- MessagesActivity
- LoginActivity
- RegisterActivity
The LoginActivity содержит 2 EditTexts и CheckBox , который сохраняет пользователя в системе. И, конечно, 2 Кнопки : одна для входа в систему, а другая для регистрацииaccount.
Я добавил intent
в LoginActivity , поэтому при входе в систему отображается MessagesActivity .И еще intent
в MessagesActivity для при чтении из SharedPreferences
, если CheckBox не проверен, переключение на LoginActivity .
Проблема в том, что я не знаю, как это сделать.Я все еще новичок в SharedPreferences
.И даже когда вход в систему и входные данные верны, приложение не переключается на MessagesActivity .Он снова показывает LoginActivity .
Мне нужна помощь, пожалуйста, если кто-нибудь знает, как это сделать.
RegisterActivity.java
public class RegisterActivity extends AppCompatActivity {
private EditText registerUsername;
private EditText registerEmail;
private EditText registerPassword;
private EditText registerConfirmPassword;
private Button registerRegisterButton;
private Button registerLoginButton;
private ProgressBar registerProgressBar;
private FirebaseFirestore firebaseFirestore;
private String userID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
registerUsername = findViewById(R.id.register_username);
registerEmail = findViewById(R.id.register_email);
registerPassword = findViewById(R.id.register_password);
registerConfirmPassword = findViewById(R.id.register_confirm_password);
registerRegisterButton = findViewById(R.id.register_register_button);
registerLoginButton = findViewById(R.id.register_login_button);
registerProgressBar = findViewById(R.id.register_progressBar);
firebaseFirestore = FirebaseFirestore.getInstance();
registerLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent loginIntent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(loginIntent);
finish();
}
});
registerRegisterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = registerUsername.getText().toString();
String email = registerEmail.getText().toString();
String password = registerPassword.getText().toString();
String confirmPassword = registerConfirmPassword.getText().toString();
if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(email) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(confirmPassword)) {
if (password.equals(confirmPassword)) {
registerProgressBar.setVisibility(View.VISIBLE);
Map<String, String> usersMap = new HashMap<>();
usersMap.put("username", username);
usersMap.put("email", email);
usersMap.put("password", password);
userID = registerUsername.getText().toString();
firebaseFirestore.collection("Users").document(userID).set(usersMap).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toasty.success(RegisterActivity.this, "Successfully Registered", Toast.LENGTH_SHORT).show();
Intent messagesIntent = new Intent(RegisterActivity.this, MessagesActivity.class);
startActivity(messagesIntent);
finish();
registerProgressBar.setVisibility(View.INVISIBLE);
}
});
} else {
Toasty.error(RegisterActivity.this, "Passwords Don't Match", Toast.LENGTH_SHORT).show();
}
}
}
});
}}
LoginActivity.java
public class LoginActivity extends AppCompatActivity {
private EditText loginUsername;
private EditText loginPassword;
private CheckBox loginKeepSignedIn;
private Button loginLoginButton;
private Button loginRegisterButton;
private ProgressBar loginProgressBar;
private FirebaseFirestore firebaseFirestore;
private String userID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginUsername = findViewById(R.id.login_username);
loginPassword = findViewById(R.id.login_password);
loginKeepSignedIn = findViewById(R.id.login_keep_signed_in);
loginLoginButton = findViewById(R.id.login_login_button);
loginRegisterButton = findViewById(R.id.login_register_button);
loginProgressBar = findViewById(R.id.login_progressBar);
firebaseFirestore = FirebaseFirestore.getInstance();
if (loginKeepSignedIn.isChecked()) {
SharedPreferences preferences = getSharedPreferences("Preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("keepSignedIn", true);
editor.apply();
}
loginRegisterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(registerIntent);
finish();
}
});
loginLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String username = loginUsername.getText().toString();
final String password = loginPassword.getText().toString();
if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
loginProgressBar.setVisibility(View.VISIBLE);
userID = loginUsername.getText().toString();
firebaseFirestore.collection("Users").document(userID).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
String userUsername = task.getResult().getString("username");
String userPassword = task.getResult().getString("password");
if (userUsername.equals(username) && userPassword.equals(password)) {
Toast.makeText(LoginActivity.this, "Everything is equal", Toast.LENGTH_SHORT).show();
Toasty.info(LoginActivity.this, "Switching to Messages Activity", Toast.LENGTH_SHORT).show();
Intent messagesIntent = new Intent(LoginActivity.this, MessagesActivity.class);
startActivity(messagesIntent);
LoginActivity.this.finish();
} else {
Toast.makeText(LoginActivity.this, "There is something not equal", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(LoginActivity.this, "Task is not successful",Toast.LENGTH_SHORT).show();
}
loginProgressBar.setVisibility(View.INVISIBLE);
}
});
}
}
});
}}
MessagesActivity.java
public class MessagesActivity extends AppCompatActivity {
private Toolbar messagesToolbar;
private Button logoutBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messages);
messagesToolbar = findViewById(R.id.messages_toolbar);
setSupportActionBar(messagesToolbar);
getSupportActionBar().setTitle("Messages");
logoutBtn = findViewById(R.id.logout);
SharedPreferences preferences = getSharedPreferences("Preferences", MODE_PRIVATE);
boolean keepSignedIn = preferences.getBoolean("keepSignedIn", false);
if (!keepSignedIn) {
Intent loginIntent = new Intent(MessagesActivity.this, LoginActivity.class);
startActivity(loginIntent);
finish();
}
logoutBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent loginIntent = new Intent(MessagesActivity.this, LoginActivity.class);
startActivity(loginIntent);
finish();
}
});
}
}