How to control storing user data using SharedPreferences when logging in and out?

北城余情 提交于 2019-11-28 02:26:00

create an AppPreference class :-

public class AppPrefrences {

    private static SharedPreferences mPrefs;
    private static SharedPreferences.Editor mPrefsEditor;

    public static boolean isUserLoggedOut(Context ctx) {
        mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
        return mPrefs.getBoolean("id_logged_in", true);
    }

    public static void setUserLoggedOut(Context ctx, Boolean value) {
        mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
        mPrefsEditor = mPrefs.edit();
        mPrefsEditor.putBoolean("id_logged_in", value);
        mPrefsEditor.commit();
    }

    public static String getUserName(Context ctx) {
        mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
        return mPrefs.getString("name", "");
    }

    public static void setUserName(Context ctx, String value) {
        mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
        mPrefsEditor = mPrefs.edit();
        mPrefsEditor.putString("name", value);
        mPrefsEditor.commit();
    }
}

and now set details in AppPreference Class:-

AppPreference.setUserLoggedOut(this, false);
AppPreference.setUserName(this, "pass username here");

and get username like this:-

AppPreference.getUserName(this);

or check user is logged or not on splash screen :-

if (isUserLoggedOut(StartActivity.this)) {
                    //user not logged in
                } else {
                   //User is logged in
                }

You have to commit or apply changes made in editor. So after

        editor.putString("username", username);
        editor.putString("password", password);
        editor.apply(); // or editor.commit()

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

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);
        editor.apply(); //This line will make necessary changes in SharedPreferences file
        startActivity(loginIntent);
        finish();
    }

add the same line on button_logout click

Moreover, 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 Object

Create a User class

class User{
    String userName;
    String password;
}

When user clicks on the login button:

String userName = editText_username.getText().toString().trim();
String password = editText_username.getText().toString().trim();

doLogin(new User(userName,password);

Then using PowerPreference you can store the user object in shared preference

public void doLogin(User user) {
   PowerPreference.getDefaultFile().putObject("key",user);
   Intent loginIntent = new Intent(LoginActivity.this, HomeActivity.class);           
   startActivity(loginIntent);
   finish();
}

Next time the user enters login screen onCreate() use

User user = PowerPreference.getDefaultFile().getObject("key",User.class)
if (user!=null){
  doLogin(user.userName,user.password)
}

To be able using PowerPreference add this to you gradle

implementation 'com.aliassadi:power-preference-lib:1.4.1'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!