Delete a named preferences file in Android

I can create a new preferences file by calling Context.getSharedPreferences(String name, int mode), putting some values in the returned preferences' editor, and committing. However, I don't see an interface in the API to remove the created file when I no longer need it. I've tried Context.deleteFile(String name), passing in the same name argument from above, but that returns false, indicating the file wasn't actually deleted. I'm using MODE_PRIVATE, if that makes a difference.

Browsing the source for context's implementation of the editor doesn't reveal any clues either, as far as I can tell.

Any ideas? The background here is that I'm allowing my users to save copies of their "current" preferences so that they can restore previously saved settings. When they don't want those settings anymore, I want to remove the saved preferences file so it doesn't take up space. At the moment the best I can do is an edit().clear().commit on the file, which I believe will remove most of the contents. Not as good as delete, though.

Answers


I've tried Context.deleteFile(String name), passing in the same name argument from above, but that returns false, indicating the file wasn't actually deleted.

That's because the file is not in that directory.

The background here is that I'm allowing my users to save copies of their "current" preferences so that they can restore previously saved settings.

How are you making the copy?

From the standpoint of supported APIs, you are best served by making the real SharedPreferences be the current active settings, and do your backups and restores to your own file format in your own directory.

If you are disinclined to go in that direction, the file will be an XML file in the ../shared_prefs directory relative to getFilesDir(), so you can construct an appropriate File object and delete it that way. However, bear in mind that Android could conceivably change this location later, or even change the storage model, and so you may run into compatibility problems in the future.


Need Your Help

What could be the problem with this array?

php arrays

This is the source $pData array I have:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.