clear the value of sharedpreferences

心不动则不痛 提交于 2020-08-26 09:34:13

问题


i am kind of new to android programming , i am designing a program that has login page and main page.When i sign in for the first time with a username and password, i created a sharedpreferences to remember that username and whenever i enter program , it skips login page and redirects me to main page.What i want to do is, when i press logout button in login page ,i want sharedpreferences forget the value of username in my database(it wont delete it from database) and force me to login me again with the same or different username and password , and i dont want to be redirected to main page when i enter application.I found something like SharedPreferences.Editor.remove() , SharedPreferences.Editor.clear(),commit() ,etc.. But code didnt work.Can you help?

public static class SaveSharedPreference {
        static final String PREF_USER_NAME = "username";

    static SharedPreferences getSharedPreferences(Context ctx) {
        return PreferenceManager.getDefaultSharedPreferences(ctx);
    }

    public static void setUserName(Context ctx, String userName) {
        SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
        editor.putString(PREF_USER_NAME, userName);
        editor.commit();
    }

    public static String getUserName(Context ctx) {
        return getSharedPreferences(ctx).getString(PREF_USER_NAME, "");
    }
}

Button btnSignIn, btnSignUp;
    LoginDataBaseAdapter loginDataBaseAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_uye_kayit_giris);





    if (SaveSharedPreference.getUserName(UyeKayitGirisActivity.this).length() == 0) {
        Intent i = new Intent(UyeKayitGirisActivity.this, MainActivity.class);
        startActivity(i);
        // finish();
    }



        // create a instance of SQLite Database
        loginDataBaseAdapter = new LoginDataBaseAdapter(this);
        loginDataBaseAdapter = loginDataBaseAdapter.open();

        // Get The Refference Of Buttons
        btnSignIn = (Button) findViewById(R.id.buttonSignIN);
        btnSignUp = (Button) findViewById(R.id.buttonSignUP);

        // Set OnClick Listener on SignUp button
        btnSignUp.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub

                /// Create Intent for SignUpActivity  abd Start The Activity
                Intent intentSignUP = new Intent(getApplicationContext(), SignUPActivity.class);
                startActivity(intentSignUP);
            }
        });




    Button btn_exit;
    // super.onCreate(savedInstanceState);
    //   setContentView(R.layout.main1);

    btn_exit = (Button) findViewById(R.id.buttonLogOUT);
    btn_exit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

// WHAT SHOULD I WRITE IN HERE?????????????


            Toast.makeText(UyeKayitGirisActivity.this, "ÜYE GİRİŞİ TEKRAR ZORUNLU HALE GETİRİLDİ!!!", Toast.LENGTH_LONG).show();
        }
    });


}

回答1:


Try like this

SharedPreferences pref = mContext.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
Editor editor = pref.edit();
editor.clear();
editor.commit();



回答2:


Here is the Method

 private void removePreference(Context context, String prefsName, String key) {
    SharedPreferences preferences = context.getSharedPreferences(prefsName, Activity.MODE_PRIVATE);
    android.content.SharedPreferences.Editor editor = preferences.edit();
    editor.remove(key);
    editor.apply();
}

You can call it like:

public void removeUser() {
    removePreference(context, FILENAME, KEY_USER);
}



回答3:


Since you have mentioned that you do NOT want to delete the value of login from shared preference but only ask for login page each time, I believe you are looking for something where you want to clear application cache programatically.

Please check, Clear Cache in Android Application programmatically and Clear Application's Data Programmatically

It is also worth reading this wonderful answer - Don't delete database or shared Preference on clear app




回答4:


To remove them all SharedPreferences.Editor.clear() followed by a commit()



来源:https://stackoverflow.com/questions/36442047/clear-the-value-of-sharedpreferences

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