• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Changing keys in SharedPreferences?

RhinoCan

Well-Known Member
I've been eager to add some preferences to my app for a while now and finally started looking into it the other day. Several videos into my research, one of the presenters mentioned that Android Studio has a bunch of templates which I'd never really noticed before, including a SettingsActivity template. Just out of curiousity, I clicked on that in my system and suddenly found several new items in my project, including 4 XML files and two new classes. Looking at the general_preferences.xml and comparing it to what I'd already read, I found it fairly eas to modify that file to obtain the preferences I want for my app. After the initial coding, I started tweaking as we probably all do, making it better and better. In one of my improvements, I changed the key for a couple of preferences to better names. Now my code doesn't work any more because it doesn't see the new key names.

I eventually figured out that changing my keys back to their old values worked and now my program works again. But I have to believe it is possible to change the names of preference keys *somehow*. Unfortunately, the documentation is pretty skimpy to say the least and I can think of no reasonable way to change my key names. Can anyone enlighten me on the proper technique? Do I need to abandon shared preferences and go with a named Preferences file instead that uses the key names I want? Do I have to add a second copy of the preferences I want to change so that I have the original and then a copy that is the same except for the key name? Do I have to somehow erase the shared preferences file of keys, then build it again? Do I have to find the shared preferences file on my file system and edit it manually? I vaguely remember having to do something like that back when I was writing Java for Windows and tinkered with Preferences.
 
Thank you for telling me about this technique!

So, if I'm understanding correctly, I can make the necessary changes by executing this code (just once because it will fail if I run it a second time):
Java:
        /*
         * TEMPORARY! - Fix keys in SharedPreferences
         */
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        //Get current values of old keys
        String userName = sharedPreferences.getString("name_text", "");
        String dbPath = sharedPreferences.getString("data_text", "");
      
        //Set new keys equal to the values from the corresponding old keys
        SharedPreferences.Editor spe = sharedPreferences.edit();
        spe.putString("user_name", userName);
        spe.putString("database_path", dbPath);
      
        //Remove old keys
        spe.remove("name_text");
        spe.remove("data_text");
      
        //Commit the changes
        spe.commit();

Naturally, I'll need to change the key names if general_preferences.xml to the new values of the key names, too.

Is that right?
 
In case anyone's wondering, I executed the code in my previous example and it seems to have worked fine.
 
Back
Top Bottom