Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6249885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:19:17+00:00 2026-05-24T13:19:17+00:00

I tried to find out how to safely assert that preferences aren’t stored anywhere.

  • 0

I tried to find out how to safely assert that preferences aren’t stored anywhere. It seems that haven’t understood the caching mechanism yet and the docs don’t clarify it. This is what I did:

File prefsFile = new File("/data/data/"+context.getPackageName() + "/shared_prefs/"
    + context.getPackageName() + "_preferences.xml");
prefsFile.delete();
assertFalse(prefsFile.exists()); // success

// This assertion could fail - why?
assertEquals(0, context.getSharedPreferences(context.getPackageName()+"_preferences",
    MODE_PRIVATE).getAll().size());

Although I deleted the prefs file, it is still possible that getAll().size() returns a non-zero value.

Could someone explain why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:19:17+00:00Added an answer on May 24, 2026 at 1:19 pm

    I tried to find out how to safely assert that preferences aren’t stored anywhere.

    Why?

    Could someone explain why?

    Off the top of my head:

    • You are not actually testing to see if the file exists before deleting it, or if the directory exists
    • You are hard-coding a path that may not be correct on all devices
    • You are assuming that deleting a file deletes the in-process copy of the SharedPreferences
    • You are not bothering to look at the HashMap returned by getAll() to see if your data is there, or if it is some system-supplied initial values

    If you want to clear out SharedPreferences, do not try deleting the file. Either call edit().clear().commit() or edit().clear().apply() on your SharedPreferences object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to find out that gd means in boost library name and I
I tried to find out this, and got that http://www.google.com/ncr uses 302(or 301) redirections(not
I tried to find out over the google but no good reference that I
I have found many similar posts and even tried to find out how to
i tried to find it out with previous answers but i cannot do it
Ive tried searching for hours now and cannot find out why my code (aka,
I've tried my best to find out a solution with the many script questions
I tried to find out how to move my working copy. I know SVN
I have tried to find out the working solution for implementing simple events booking
I tried to find out how C# goes about comparing objects in a HashSet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.