繁体   English   中英

应用程序退出时不保存SharedPreferences

[英]SharedPreferences Not Saving on App Quit

我有一个不保存共享首选项的应用程序。 我尝试使用commit()apply()来保存这些首选项,但是一旦应用程序关闭它们就不会保存。 如果我在我的应用中更改屏幕,则会进行更改,但在应用重新启动时会丢失。

应用启动时调用的静态方法:

static {
        prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.getContext());//MainActivity.getContext().getSharedPreferences("LocationTimer", Context.MODE_PRIVATE);
        editor = prefs.edit();
        if(!prefs.contains("totalLocations")) {
            prefs.edit().putInt("totalLocations", 0).commit();
        }
        if(prefs.getInt("totalLocations", 0) == 0) {
            for (int i = 1; i <= COUNT; i++) {
                addItem(createLocationItem(i));
            }
            Log.i("PREFERENCES", "CREATED 1 LOCATION");
        } else {
            for (int i = 1; i <= prefs.getInt("totalLocations", 1); i++) {
                addItem(new LocationItem(String.valueOf(i), "Location " + String.valueOf(i), prefs.getString(String.valueOf(i), "")));
            }
            Log.i("PREFERENCES", "LOADED LOCATION(S)");
        }
        Log.i("PREFERENCES", "LOADED " + String.valueOf(prefs.getInt("totalLocations", 1)) +" LOCATION(S)");
    }

记录的消息是“PREFERENCES:LOADED LOCATION(S)”而不是“PREFERENCES:CREATED 1 LOCATION”所以我知道该项目正在加载而不是简单地创建。

创建/加载任何项目时调用的方法:

public static void addItem(LocationItem item) {
        ITEMS.add(item);
        ITEM_MAP.put(item.id, item);

        saveStringToPreferences(item.id, item.details);

        editor.putInt("totalLocations", ITEMS.size());
        editor.apply();

        Log.i("PREFERENCES", "CONTAINS " + String.valueOf(prefs.getInt("totalLocations", 1)) +" LOCATION(S)");
        Log.i("ITEMS", "CONTAINS " + String.valueOf(ITEMS.size()) +" LOCATION(S)");
    }

我怎么知道这是在已经加载应用程序时保存首选项是上面的消息显示除了一个之外的其他数字。 一个是应用加载时显示的金额。

感谢您的任何帮助和建议。 如果您有任何疑问,请发布,我会尽快回复。

对于遇到此类问题的每个人,我的修复方法是将SharedPreferences放入专用的类文件中。 我创建了一个名为AssetLoader.java的文件,并将所有的SharedPreferences编辑和初始化放在他们的文件中。

这是我的AssetLoader文件:

import android.content.Context;
import android.content.SharedPreferences;

import com.xenicdev.locationtimer.MainActivity;

public class AssetLoader {
    private static SharedPreferences prefs;

    public static void load() {

        // Prefs
        prefs = MainActivity.getContext().getSharedPreferences("LocationTimer", Context.MODE_PRIVATE);
        if(!prefs.contains("totalLocations")) {
            prefs.edit().putInt("totalLocations", 0).apply();
        }
    }

    public static void setTotalLocations(int totalLocations) {
        prefs.edit().putInt("totalLocations", totalLocations).apply();
    }

    public static Integer getTotalLocations() {
        return prefs.getInt("totalLocations", 0);
    }

    public static void setItemDetails(String id, String details) {
        prefs.edit().putString(id, details).apply();
    }

    public static String getItemDetails(String id) {
        return prefs.getString(id, "");
    }
}

在主文件中创建时添加:

AssetLoader.load();

要在其他文件中调用这些方法,请添加:

AssetLoader.getTotalLocations();

使用此方法的另一个好处是,如果删除所有SharedPreferences并只是使用对AssetLoader的调用,则可以清理其他类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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