How to use the Sharedpreferencesutils.java class?

Asked

Viewed 102 times

1

Sorry about the title, I don’t know how else would be a better way to ask.

I’m learning to code on android apps, and I found a JAVA class to facilitate the use of Sharedpreferences. But I don’t quite understand how to use.

I found the code here.

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 don’t understand why he put that example there and the Prefsfile in private.

It would 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 that way, but is that right? Just that it always saves in a file called PrefsFile.xml. If I want to separate preferences I think I would need to modify this class. Does anyone have a better idea?

2 answers

0

PREF_EXAMPLE is not used in the code. The PREFS_FILE variable has been kept private only for us to change where it is saved.

What this class does is encapsulate code so you don’t need to copy and paste code snippets.

About private mode it indicates that only your application can read that information. https://developer.android.com/reference/android/content/Context.html#MODE_PRIVATE

It is worth reading about Shared preferences. Here is the link: https://developer.android.com/training/basics/data-storage/shared-preferences.html?hl=pt-br

  • You don’t understand my question, young man, I’m talking about this line here: private static final String PREFS_FILE_NAME = "PrefsFile"; be in private, because it requires all preferences to be only in a single file.

  • and on these lines public static final String PREFS_EXAMPLE = "example"; I don’t understand her usefulness in the code. And tbm about a way to make this class look better to use, and if I’m using it right. About Sharedpreferences I already know how to use, and MODE_PRIVATE as well. Still thanks for responding.

  • updated the response

0


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

If you want to be able to specify another name for the archive you should override the public methods setString(), getString() and remove() adding a parameter that receives the name of the file to use. The private method getSharedPreferencesEditor() needs to be amended to deal with that requirement.

Applying the changes and simplifications, the class will look 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 public method:

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

and

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

    Thank you young man, perfect, I have not tested, but I understood perfectly your changes and explanation, it was much better now. I will use this way. Thank you again!! bjss

Browser other questions tagged

You are not signed in. Login or sign up in order to post.