Avijit
Avijit

Reputation: 3824

Delete data from shared preferences in android

I am trying to delete the data from shared preferences. But I cant do that. To track that the data is removed or not, I am using this code:

btnLogout.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
            SharedPreferences prefs = getSharedPreferences(share_pref_file, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.remove(share_pref_file);
            editor.clear();
            editor.commit();
            getApplicationContext().getSharedPreferences(share_pref_file, 0).edit().clear().commit(); 
                String strJson = prefs.getString("jsondata","");
                if(strJson != null) 
                    {
                     Log.d("CLEAR", "cccccccccccccccccccccccccccc");
                    }
                userFunctions.logoutUser(getApplicationContext());
                Intent login = new Intent(getApplicationContext(),LoginActivity.class);
                login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(login);
                // Closing dashboard screen
                finish();
        }
});

But in logcat window it is showing me "cccccccccccccccccccccccccccc" value every time.

So can anyone help me out that how to remove/delete the data from shared preferences so that if I click on 'logout' button it will remove all the stored data? Thanks in advance.

Upvotes: 3

Views: 9575

Answers (6)

reza
reza

Reputation: 1

It is my solution:

You can put null instead of your favorite data in shareprefrences

getApplicationContext();
SharedPreferences.Editor pref = (Editor) getSharedPreferences("data", MODE_PRIVATE).edit();
pref.putString("admin_no", null);
pref.commit();

Upvotes: 0

Gunaseelan
Gunaseelan

Reputation: 15515

settings.edit().clear().commit();

is a one line code I am using, works perfectly

Upvotes: 0

Nirav Ranpara
Nirav Ranpara

Reputation: 13785

Try editor.clear(); followed by a editor.commit();

Here is one example that I've used:

Preference clearPref = (Preference) findPreference("MyPref");

   btnLogout.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        public boolean onPreferenceClick(Preference preference) {
            SharedPreferences settings =  PreferenceManager.getDefaultSharedPreferences(getBaseContext());                          
            SharedPreferences.Editor editor = settings.edit();
            editor.clear();
            editor.commit();                
            Toast.makeText(getBaseContext(), "All data cleared!", Toast.LENGTH_SHORT).show();

            return true;
        }

    });

Upvotes: 1

Siddharth Lele
Siddharth Lele

Reputation: 27748

Either change this:

String strJson = prefs.getString("jsondata", "");

To:

String strJson = prefs.getString("jsondata", null);

And then check:

if(strJson != null) {
    Log.d("CLEAR", "cccccccccccccccccccccccccccc");
}

Or keep it as it is and check it like this:

if(strJson.equals("")) {
    Log.d("CLEAR", "cccccccccccccccccccccccccccc");
}

Upvotes: 0

Yugesh
Yugesh

Reputation: 4092

Try this one inside the button click event to clear the SharedPreferences values.

Editor editor = getSharedPreferences("MyPref", Context.MODE_PRIVATE).edit();
            editor.clear();
            editor.commit();

Upvotes: 1

Tushar
Tushar

Reputation: 8049

An empty string is still a string (and String("") != null will return true). Try this instead:

if(!strJson.equals("")) 

This assumes the empty string will never be a valid input in your SharedPreferences in the first place.

Upvotes: 2

Related Questions