Y
Y
Yuri2019-10-08 14:46:04
Java
Yuri, 2019-10-08 14:46:04

How to update downloaded SharedPreference file in Android?

The task is this:
Upon request, the application downloads a file with SharedPreference AppPrefs.xml settings from a remote storage. Next, it is replaced in the memory of the smartphone. And then I create a new instance of SharedPreference AppPrefs.xml, but the trouble is that the context does not read again from memory, but simply takes the old file out of the context. Because of this, app settings are only changed after a complete restart of the app. Even the finish() method does not help, anyway, the AppPrefs.xml instance does not change in memory. How to be?

public class AppPreferenceSingleton {

    private static AppPreferenceSingleton mAppPreferenceSingleton = null;
    private static Context mContext = null;
    private static SharedPreferences mAppPref = null;
    private static SharedPreferences.Editor mEditorAppPref = null;

    public static AppPreferenceSingleton getInstance(Context context, boolean update) {
        if (mAppPreferenceSingleton == null || update) {
      
        mAppPref = null;
       mEditorAppPref  = null;     
     
        mContext = context;
       mAppPreferenceSingleton = new AppPreferenceSingleton();
        }
        return mAppPreferenceSingleton;
    }

    private AppPreferenceSingleton() {
        mAppPref = mContext.getSharedPreferences(AppConstants.APP_PREF_NAME, Context.MODE_PRIVATE);
        mEditorAppPref = mAppPref.edit();
    }

    public void createPrefs() {
        mEditorAppPref.putString("create", "ok").commit();
    }


    public void updatePrefs() {
        mEditorAppPref.putString("update", "ok").commit();
    }

    public void setUserName(String userName) {
        mEditorAppPref.putString("userName", userName);
        mEditorAppPref.commit();
    }

    public String getUserName() {
        return mAppPref.getString("userName", "Студент");
    }
}

After the download of the AppPrefs.xml file is complete, I update:
AppPreferenceSingleton.getInstance(mContext,true);
In the debugger, I saw that a new mAppPreferenceSingleton instance is created, but mAppPref is not.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis, 2019-10-09
@akaish

It never occurred to me to replace xml sp. It seems like API sp was not written for this. Plus, with a high degree of probability, when the SP object is closed, data from the SharedPreferences.Editor buffer is written to the file and it is not a fact that at the same time.
Why would you even try to use this hack? Store data as JSON and after download deserialize into POJO using, for example, GSON. Well, or use Firebase Realtime DB. Or even a pack of standard solutions. There is simply no guarantee that even if you make it work, it will work on all versions and devices.
Ah, and yes, it's good practice in SP to use apply(), commit() immediately writes the changes to the file.

Y
Yuri, 2019-10-10
@Sailoc

The only possible option, tried everything, nothing helped. The only option is through a crutch.
1. Download files from remote storage, name them differently, for example, add them to the _temp file
2. Create temporary SharedPreference objects.
3. Read all data from downloaded prefs into HashMap.
4. Copy everything from the HashMap to the working preference in the usual way through the Editor.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question