banshee1907
banshee1907

Reputation: 37

clear the value of sharedpreferences

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


}

Upvotes: 0

Views: 6907

Answers (4)

Dilip
Dilip

Reputation: 2734

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

Upvotes: 0

Natarajan Raman
Natarajan Raman

Reputation: 606

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

Upvotes: 0

Yasir Tahir
Yasir Tahir

Reputation: 800

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

Upvotes: 0

Abhishek Patel
Abhishek Patel

Reputation: 4328

Try like this

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

Upvotes: 2

Related Questions