How to use the SharedPreferencesUtils.java class?

1

Sorry for the title, I do not know how it would be a better way to ask.

I'm learning to program in android applications, and found a class in JAVA to make it easier to use SharedPreferences . But I did not quite understand how to use it.

I found the here code

And the code is this:

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

/**
 * Copyright (C) 2016 Mikhael LOPEZ
 * Licensed under the Apache License Version 2.0
 * Utility class for the SharedPreferences management
 */
public class SharedPreferencesUtils {

    // PUBLIC PREF NAME
    public static final String PREFS_EXAMPLE = "example";

    //region Singleton Shared Preferences
    private static final String PREFS_FILE_NAME = "PrefsFile";
    private static SharedPreferences mSharedPreferences;

    private static SharedPreferences getSharedPreferencesEditor(Context context) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences(PREFS_FILE_NAME, Context.MODE_PRIVATE);
        }
        return mSharedPreferences;
    }
    //endregion

    public static void setString(Context context, String name, String value) {
        SharedPreferences.Editor editor = getSharedPreferencesEditor(context).edit();
        editor.putString(name, value);
        editor.commit();
    }

    public static String getString(Context context, String name) {
        return getSharedPreferencesEditor(context).getString(name, null);
    }

    public static void remove(Context context, String name) {
        getSharedPreferencesEditor(context).edit().remove(name).commit();
    }

}

I did not understand why he put that example there and PrefsFile in private .

Would it be better to increment this code to change this data with a constructor?

I'm using it this way for now:

SharedPreferencesUtils.setString(getApplicationContext(), "userLogged", varuserlogged);

and

UsernameLogged = SharedPreferencesUtils.getString(getApplicationContext(),"userLogged");

It's working like this, but is that right? Only It always saves in a file named PrefsFile.xml . If I want to separate the preferences I think I would need to modify this class. Anyone have any better ideas?

    
asked by anonymous 23.02.2018 / 02:44

2 answers

0

The file name used is the one the class declaration assigned to the PREFS_FILE_NAME attribute.

If you want to be able to indicate another name for the file you should overwrite the public methods setString() , getString() and remove() by adding a parameter that receives the file name to use. The private method getSharedPreferencesEditor() needs to be changed in order to handle this requirement.

Applying the changes and simplifications, the class looks like this:

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

public class SharedPreferencesUtils {

    private static final String DEFAULT_PREFS_FILE_NAME = "PrefsFile";

    private static SharedPreferences getSharedPreferencesEditor(Context context, String fileName) {    
        return context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
    }

    public static void setString(Context context, String name, String value) {
        setString(context, name, value, DEFAULT_PREFS_FILE_NAME);
    }

    public static void setString(Context context, String name, String value, String fileName) {
        SharedPreferences.Editor editor = getSharedPreferencesEditor(context, fileName).edit();
        editor.putString(name, value);
        editor.commit();
    }


    public static String getString(Context context, String name) {
        return getString(context, name, DEFAULT_PREFS_FILE_NAME);
    }

    public static String getString(Context context, String name, String fileName) {
        return getSharedPreferencesEditor(context, fileName).getString(name, null);
    }

    public static void remove(Context context, String name) {
        remove(context, name, DEFAULT_PREFS_FILE_NAME);
    }

    public static void remove(Context context, String name, String fileName) {
        getSharedPreferencesEditor(context, fileName).edit().remove(name).commit();
    }
}

When you want to use a different file, pass its name to the last parameter of each of the public methods:

SharedPreferencesUtils.setString(getApplicationContext(), "userLogged", varuserlogged, "NomeDoArquivo");

and

UsernameLogged = SharedPreferencesUtils.getString(getApplicationContext(),"userLogged", "NomeDoArquivo");
    
23.02.2018 / 15:15
0

PREF_EXAMPLE is not used in code. The variable PREFS_FILE was kept private only to change the location where it is saved.

What this class does is encapsulate code so you do not have to copy and paste code snippets.

About private mode it indicates that only your application can read that information. link

It pays to read about shared preferences. Here's the link: link

    
23.02.2018 / 02:56