0

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


}
banshee1907
  • 27
  • 1
  • 7

4 Answers4

2

Try like this

SharedPreferences pref = mContext.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
Editor editor = pref.edit();
editor.clear();
editor.commit();
Abhishek Patel
  • 4,280
  • 1
  • 24
  • 38
0

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

Community
  • 1
  • 1
Natarajan Raman
  • 606
  • 1
  • 4
  • 14
0

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);
}
Yasir Tahir
  • 790
  • 1
  • 11
  • 31
0

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

Dilip
  • 2,622
  • 1
  • 20
  • 27
  • SharedPreferences preferences = getSharedPreferences("Mypref", 0); preferences.edit().remove("shared_pref_key").commit(); – Dilip Apr 26 '16 at 06:02