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();
}
});