I have tried many times with different ways but the checkbox to remember the login details is still not working. When I close the app and open the app, it's still in the login activity, even though I checked the remember checkbox before, I don't know where I made an error or am missing another step.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnRegister = (Button) findViewById(R.id.buttonSignup);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(i);
}
});
sharedPreferences = MainActivity.this.getSharedPreferences("Data", MODE_PRIVATE);
editor = sharedPreferences.edit();
boolean login = sharedPreferences.getBoolean("ISLOGGEDIN", false);
if (login == true){
edtAccount.setText(sharedPreferences.getString("account",""));
edtPassword.setText(sharedPreferences.getString("password",""));
checkBoxRemember.setChecked(true);
}
edtAccount = (EditText) findViewById(R.id.editTextAccount);
edtPassword = (EditText) findViewById(R.id.editTextPassword);
btnLogin = (Button) findViewById(R.id.buttonLogin);
checkBoxRemember = (CheckBox) findViewById(R.id.checkBoxRemember);
db=new Database(this);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String account = edtAccount.getText().toString();
String password = edtPassword.getText().toString();
if (account.equals("") || password.equals("")){
Toast.makeText(MainActivity.this,
"Không được để trống tài khoản và mật khẩu",
Toast.LENGTH_SHORT).show();
}
else {
Boolean result = db.checkUserNamePassword(account, password);
if(result==true) {
if(checkBoxRemember.isChecked()){
editor.putBoolean("ISLOGGEDIN",true);
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
intent.putExtra("account", account);
SharedPreferences sharedPref = getSharedPreferences("account", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("account", account);
editor.putString("password", password);
editor.commit();
startActivity(intent);
MainActivity.this.finish();
} else {
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
intent.putExtra("account", account);
SharedPreferences sharedPref = getSharedPreferences("account", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("account", account);
editor.putString("password", password);
editor.putBoolean("ISLOGGEDIN",true);
editor.clear();
editor.commit();
startActivity(intent);
MainActivity.this.finish();
}
}
else{
Boolean userCheckResult = db.checkUserName(account);
if(userCheckResult == true){
Toast.makeText(MainActivity.this,
"Sai mật khẩu!", Toast.LENGTH_SHORT).show();
edtPassword.setError("invalid password!");
} else{
Toast.makeText(MainActivity.this,
"Account does not exist", Toast.LENGTH_SHORT).show();
edtAccount.setError("Tài khoản không tồn tại!");
}
}
}
}
});
}
Or is there a command in the database or elsewhere that affects this?
Edit
Once the user logs in, how can I have them log out again? The following doesn't work:
final AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
builder.setMessage(R.string.logout_message);
builder.setNegativeButton("Đăng xuất", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(HomeActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
CodePudding user response:
The problem is that you are checking for "ISLOGGEDIN" in this SharedPref
sharedPreferences = getSharedPreferences("Data", MODE_PRIVATE);
but saving your values to this one (with a different name)
SharedPreferences sharedPref = getSharedPreferences("account", MODE_PRIVATE);
A few things to change to fix/clean this up:
- Use the same
SharedPreferences
database in both places - Fix the
if(login)
clause to not use null views and to forward you on to the HomeActivity (as I think you intend) - Just check if the "account" string is present instead of adding a separate boolean
That would look something like this:
// Just get a single SharedPreferences to use
sharedPreferences = getSharedPreferences("AccountPrefs", MODE_PRIVATE);
// Don't need a separate bool if you can just check the "account" string
String account = sharedPreferences.getString("account","");
if (!account.isEmpty()) {
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
intent.putExtra("account", account);
startActivity(intent);
finish();
// If you don't want to auto-forward to HomeActivity, you could
// remove the code above and fill in the views here instead.
// Just make sure you call findViewById to set them before
// this point so they are not null
}
and when you save the logged-in state
Boolean loginIsOk = db.checkUserNamePassword(account, password);
if(loginIsOk) {
SharedPreferences.Editor editor = sharedPreferences.edit();
if(checkBoxRemember.isChecked()) {
editor.putString("account", account);
editor.putString("password", password);
} else {
editor.clear();
}
editor.apply();
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
intent.putExtra("account", account);
startActivity(intent);
finish();
}
else {
// show toasts
}
To log out (from some other activity like HomeActivity) you would just clear the shared preferences out and launch the login activity, like this
private void logOut() {
// Clear the prefs
SharedPreferences prefs = getSharedPreferences("AccountPrefs", MODE_PRIVATE);
prefs.edit().clear().apply();
// Use the flags to make sure the
// user can't press "back" to come back here after logging out
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}