简体   繁体   中英

Misbehavior with sharedpreferences

Before you judge me, I'd like to say I read theese:

But I still can't understand, can't get things work. I get totally misbehavior of my preferences. My code:

public static SharedPreferences sharedAppPreferences;
public static final String AppsListKey = "AppListKey";
public static final String AppsPreferences = "AppsPreferences";
public static ArrayList<String> packageNames;

public void chooseApps(View view) {
        sharedAppPreferences = getSharedPreferences(AppsPreferences, Context.MODE_PRIVATE);
        if (sharedAppPreferences.contains(AppsListKey)) {
            Set<String> buffer = new LinkedHashSet<String>(sharedAppPreferences.getStringSet(AppsListKey, new LinkedHashSet<String>()));
            packageNames = new ArrayList<String>(buffer);
        } else {
            packageNames = new ArrayList<String>();
        }
        PackageManager packageManager = getPackageManager();

        int flags = PackageManager.GET_META_DATA | PackageManager.GET_SHARED_LIBRARY_FILES | PackageManager.GET_UNINSTALLED_PACKAGES;
        List<ApplicationInfo> packageList = packageManager.getInstalledApplications(flags);

        for (ApplicationInfo pack : packageList) {

            if (((pack.flags & ApplicationInfo.FLAG_SYSTEM) == 1) || packageNames.contains(pack.loadLabel(packageManager).toString())) {
                // System application or already in array
            } else {
                // Installed by user and isnt in array
                packageNames.add(pack.loadLabel(packageManager).toString());
            }
        }
        Editor editor = sharedAppPreferences.edit();
        Set<String> buffer1 = new LinkedHashSet<String> (packageNames);
        editor.putStringSet(AppsListKey, buffer1);
        editor.commit();
        //packageNames.clear();
        //buffer1.clear();
        buffer1 = new LinkedHashSet<String>(sharedAppPreferences.getStringSet(AppsListKey, new LinkedHashSet<String>()));
        packageNames = new ArrayList<String>(buffer1);
        AppList appList = new AppList();
        appList.show(getSupportFragmentManager(), "AppList");
    }

Why first time I run my app I get list like

[Skype, Facebook, Whatsapp, Twitter, Google+]

It's ok as long as app is running... But if I kill my app and restart I get now totally different list like

[Whatsapp, Google+, Skype, Twitter, Facebook]

Could someone explain me please what is here wrong?

The only difference in your list before and after, is the ordering...

To expand on what I have been talking about, I just realised your mistake..

LinkedHashSet is ordered. However, you are storing in

Set<String> buffer 

Set<String> is not ordered...

So it jumbles it up again.

You need to store it in parameter of LinkedHashSet like below

LinkedHashSet<String> buffer = new LinkedHashSet<String>(sharedAppPreferences.getStringSet(AppsListKey, new LinkedHashSet<String>()));

(There are two occasions i see this needs to be changed. )

Edit:

One final thing you can do to help, is rather than create a new list, cast the existing one from shared prefs ...

 LinkedHashSet<String> buffer = (LinkedHashSet<String>)sharedAppPreferences.getStringSet(AppsListKey, new LinkedHashSet<String>());

There are alternatives to HashSets here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM