PrefsUtils is a utility library for simplified and flexible access to Android's SharedPreferences. It provides an easy-to-use API to save, retrieve, and manage key-value pairs, including primitive types, objects, and lists.
- Singleton pattern for global access.
- Supports all basic types: boolean, int, float, long, and String.
- Save and retrieve custom objects and lists using Gson serialization.
- Key existence checks, deletion, and clearing of preferences.
- Lightweight and fast.
-
Add PrefsUtils to your project: Include the
PrefsUtilsclass in your project directory. -
Initialize in Application class:
import android.app.Application; import com.nexora.prefsutils.PrefsUtils; public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); PrefsUtils.initialize(this); } }
PrefsUtils prefs = PrefsUtils.getInstance();// Save a boolean value
prefs.putBoolean("isLoggedIn", true);
// Retrieve a boolean value
boolean isLoggedIn = prefs.getBoolean("isLoggedIn", false);// Save an integer value
prefs.putInt("userAge", 25);
// Retrieve an integer value
int userAge = prefs.getInt("userAge", 0);// Save a float value
prefs.putFloat("userScore", 85.5f);
// Retrieve a float value
float userScore = prefs.getFloat("userScore", 0.0f);// Save a long value
prefs.putLong("lastLogin", System.currentTimeMillis());
// Retrieve a long value
long lastLogin = prefs.getLong("lastLogin", 0L);// Save a string value
prefs.putString("username", "JohnDoe");
// Retrieve a string value
String username = prefs.getString("username", "");User user = new User("John", 25);
prefs.putObject("userProfile", user);User user = prefs.getObject("userProfile", User.class);List<User> userList = Arrays.asList(new User("John", 25), new User("Jane", 30));
prefs.putObjectList("userList", userList);List<User> userList = prefs.getObjectList("userList", User.class);boolean exists = prefs.contains("username");boolean removed = prefs.remove("username");prefs.clearAll();If PrefsUtils is not initialized, the following exception will be thrown:
throw new PrefsUtilsException("PrefsUtils must be initialized using PrefsUtils.initialize(context) before usage.");Ensure you call PrefsUtils.initialize(context) in the Application class before using the library.
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.nexora.prefsutils.PrefsUtils;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PrefsUtils prefs = PrefsUtils.getInstance();
// Save data
prefs.putBoolean("isLoggedIn", true);
prefs.putString("username", "JohnDoe");
// Retrieve data
boolean isLoggedIn = prefs.getBoolean("isLoggedIn", false);
String username = prefs.getString("username", "");
// Logging values
System.out.println("Logged In: " + isLoggedIn);
System.out.println("Username: " + username);
}
}This library is developed by Nexora Technologies and is free to use and distribute under the MIT License.