I'm trying to create an app which uses username and password to login, then stay logged in as long as user didn't logout -or didn't delete app data of course-, and as far as I know that SharedPreferences is the best to do so. How do I implement it correctly?
I've tried to create SharedPreferences object then Editor object to check at launching app if there are data stored for username and password, and if so then login automatically. Then for logging out, once is logout button is clicked, username and password keys are deleted from SharedPreferences. But I'm not sure, I guess I've done it in a wrong way so the app doesn't work.
Here is a simple example of what I want to make(assume all XML files and IDs are right because the app was working fine before adding SharedPreferences):
LoginActivity.java:
public class LoginActivity extends AppCompatActivity {
private Button button_login;
private EditText editText_username;
private EditText editText_password;
private SharedPreferences pref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
perf = getApplicationContext().getSharedPreferences("user_pref", 0);
SharedPreferences.Editor editor = perf.edit();
if(!(sharedPref.getString("username", null)).isEmpty() && !(sharedPref.getString("password", null)).isEmpty()){
doLogin(sharedPref.getString("username", null), sharedPref.getString("password", null));
}
//define editText_username, editText_password and button_login
button_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!TextUtils.isEmpty(editText_username.getText()) && !TextUtils.isEmpty(editText_password.getText())){
doLogin(editText_username.getText().toString().trim(), editText_password.getText().toString().trim());
}
}
});
}
public void doLogin(String username, String password) {
Intent loginIntent = new Intent(LoginActivity.this, HomeActivity.class);
SharedPreferences.Editor editor = perf.edit();
editor.putString("username", username);
editor.putString("password", password);
startActivity(loginIntent);
finish();
}
}
HomeActivity.java:
public class HomeActivity extends AppCompatActivity {
private SharedPreferences perf;
private Button button_logout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
perf = getApplicationContext().getSharedPreferences("user_pref", 0);
//button_logout define
button_logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = perf.edit();
editor.remove("username");
editor.remove("password");
Intent logoutIntent = new Intent(HomeActivity.this, LoginActivity.this);
startActivity(logoutIntent);
finish();
}
});
}
I don't get any results, it crashes. So I'm not sure if I did it correctly or not.
What you have done is the right, the only thing is you have not committed your preference values.
So your function will look like this
add the same line on
button_logout
clickMoreover, I will recommend you to create a utility class for SharedPreference operations.
Make it simple using
PowerPreference
so you can store the User as an ObjectCreate a
User
classWhen user clicks on the login button:
Then using
PowerPreference
you can store the user object in shared preferenceNext time the user enters login screen onCreate() use
To be able using
PowerPreference
add this to you gradlecreate an AppPreference class :-
and now set details in AppPreference Class:-
and get username like this:-
or check user is logged or not on splash screen :-
You have to commit or apply changes made in editor. So after