I wrote the shared preferences code and it did not work for me, I want it to move to a specific activity when the name is academic and the password is 123, and I want it to move to another activity when the name is admission and the password is 456. Where is the problem?
//this is my code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login = findViewById(R.id.btnLogin);
name = findViewById(R.id.inputUsername);
password = findViewById(R.id.inputPassword);
checkBox = findViewById(R.id.checkBox);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences preferences = getSharedPreferences("login data", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
if (checkBox.isChecked()) {
editor.putString("username", name.getText().toString());
editor.putString("MyPassword", password.getText().toString());
editor.putBoolean("checked", true);
editor.apply();
} else {
editor.remove("username");
editor.remove("MyPassword");
editor.remove("checked");
editor.apply();
}
if (TextUtils.isEmpty(name.getText().toString()) || TextUtils.isEmpty(password.getText().toString())) {
Toast.makeText(LoginActivity.this, "empty data", Toast.LENGTH_SHORT).show();
}
if (name.getText().toString().equals(true_user_name_Admission) && password.getText().toString().equals(true_password_Admission)) {
Intent l = new Intent(LoginActivity.this, AdmissionDashboardActivity.class);
startActivity(l);
Toast.makeText(LoginActivity.this, "Accessed", Toast.LENGTH_SHORT).show();
} else {
if (name.getText().toString().equals(true_user_name_Academic) && password.getText().toString().equals(true_password_Academic)) {
Intent ad = new Intent(LoginActivity.this, AcademicDashboardActivity.class);
startActivity(ad);
Toast.makeText(LoginActivity.this, "Accessed", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, "Required", Toast.LENGTH_SHORT).show();
name.setError("Required");
password.setError("Required");
}
}
}
});
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences sp = getSharedPreferences("login data", MODE_PRIVATE);
name.setText(sp.getString("username", ""));
password.setText(sp.getString("MyPassword", ""));
checkBox.setChecked(sp.getBoolean("checked", false));
}
}