Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

android sharedpreferences get null when app or phone restart

I have a sharedpreferences which save and retrieve data correct. but problem is this when phone restart it give null value.

here is code which I have try

if(encodedImage_profile3!=null)
        {

            wallpaper_sharedprefrences_profile3 = PreferenceManager.getDefaultSharedPreferences(context);
            SharedPreferences.Editor edit = wallpaper_sharedprefrences_profile3.edit();
            edit.putString(PROFILE2_WALLPAPER, encodedImage_profile3);
            edit.commit();
            //  Toast.makeText(context, "new same image in prefrences", Toast.LENGTH_SHORT).show();
        }
        wallpaper_sharedprefrences_profile3 = PreferenceManager.getDefaultSharedPreferences(context);

        final String load_wallpaper_profile3 =wallpaper_sharedprefrences_profile3.getString(PROFILE2_WALLPAPER, "");

I have also try this

 if(encodedImage_profile3!=null)
        {
            wallpaper_sharedprefrences_profile3 = context.getSharedPreferences(PREF_PROFILE2_WALLPAPER, Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = wallpaper_sharedprefrences_profile3.edit();
            edit.putString(PROFILE2_WALLPAPER, encodedImage_profile3);
            edit.commit();
        }
        wallpaper_sharedprefrences_profile3 = context.getSharedPreferences(PREF_PROFILE2_WALLPAPER, Context.MODE_PRIVATE);
        final String load_wallpaper_profile3 =wallpaper_sharedprefrences_profile3.getString(PROFILE2_WALLPAPER, "");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Rather than hard-coding shared preference logic , create a Utility class like following and access it throughout your project. Separate methods are available for different data types

/**
 * Shared preference class to handle app's local content
 */
public class SharedPreferenceHandler {

    //Class variables
    private SharedPreferences sharedPrefSettings;
    private SharedPreferences.Editor editor;
    private static SharedPreferenceHandler m_cPrefObj=null;

    private SharedPreferenceHandler(Context context){
        sharedPrefSettings = context.getSharedPreferences(SharedPreferenceConstants.SHARED_PREF_KEY, Context.MODE_PRIVATE);
        editor=sharedPrefSettings.edit();
    }

    public static SharedPreferenceHandler getSharedPrefInstance(Context context){
        if(m_cPrefObj==null){
            m_cPrefObj=new SharedPreferenceHandler(context);
        }
        return m_cPrefObj;
    }
    /**
        returns string value for shared preference key
     */
    public String getSharedPrefValue(String key){
        return sharedPrefSettings.getString(key,null);
    }

    /**
        returns boolean shared preference value for key
     */
    public boolean getSharedPrefValueBool(String key){
        return sharedPrefSettings.getBoolean(key, false);
    }
    /**
        sets string shared preference value for key
     */
    public void setSharedPrefBool(String key,boolean value){
        editor.putBoolean(key,value);
        editor.commit();
    }
    /**
        sets string shared preference value for key
     */
    public void setSharedPref(String key,String value){
        editor.putString(key, value);
        editor.commit();
    }

    /**
     *
     * @param key
     * @return
     */
    public long getSharedPrefValueLong(String key){
        return sharedPrefSettings.getLong(key,0);
    }

    /**
     *
     * @param key
     * @param value
     */
    public void setSharedPrefLong(String key,long value){
        editor.putLong(key,value);
        editor.commit();
    }

    /**
     *
     * @param key
     * @return
     */
    public int getSharedPrefValueInt(String key){
        return sharedPrefSettings.getInt(key,0);
    }

    /**
     *
     * @param key
     * @param value
     */
    public void setSharedPrefInt(String key,int value){
        editor.putInt(key,value);
        editor.commit();
    }
}

Inorder to access this logic in required classes,

private SharedPreferenceHandler mSharedPreferenceHandler;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initiateDependencies();

    }
    /**
     * Should initiate classes and objects which will be referred throughout app
     */
    private void initiateDependencies(){

        mSharedPreferenceHandler =SharedPreferenceHandler.getSharedPrefInstance(this);

    }

To set shared preference value do the following

mSharedPreferenceHandler.setSharedPref("PREF_KEY_REFRESH_INTERVAL", "abc");
mSharedPreferenceHandler.setSharedPrefBool("PREF_KEY_REFRESH_ACTION",true);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...