Search code examples
androidcordovatitanium-mobile

Converting Android App Preferences from Titanium to Cordova


We have an app in the store that was built with Titanium. When the user register's their data is stored locally like this:

Ti.App.Properties.setString('registration', JSON.stringify(regData));

We are updating to a new version that is built with Cordova. I am trying to load the old reg data like this:

plugins.appPreferences.fetch(
    function(response)(alert(response);},
    function(error)(alert(error);},
    'registration'
);

(This uses the plugin me.apla.cordova.AppPreferences)

If I install v1 (Titanium) and save the reg data, then update to v2 (Cordova), the error handler is called when fetching. How can I load the existing app preferences?

Note: This works as intended on iOS.


Solution

  • The AppPreferences Cordova plugin retrieves Android default preferences like this:

    PreferenceManager.getDefaultSharedPreferences(cordova.getActivity());
    

    But Titanium stores preferences under the name "titanium". So to load the old Titanium prefs, I added this code to the Cordova plugin:

    SharedPreferences tiPrefs = cordova.getActivity().getSharedPreferences("titanium", 0);
    

    This code only runs if the default preferences are null. If found, I know that the app is being updated from the old version. So, I immediately translate and re-save the preferences with the Cordova plugin. Next time the app runs, it loads the default preferences as expected by the Cordova plugin.