3

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.

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

stackoverflow.com/questions/15758775/… check this link once please.
1

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

Comments

1

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

    });

Comments

0

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

Comments

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

is a one line code I am using, works perfectly

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.