Skip to content

Library that makes easy to work with Shared Preferences

Notifications You must be signed in to change notification settings

BiAtoms/PrefsHandler

Repository files navigation


Neural Network Visualizer

A useful library containing a helper methods and making easy to work with Shared Preferences.

Demo AppKey FeaturesHow To UseUsageHow to SetupLicense

Demo App

Neural Network Visualizer

Key Features

  • Initialize PrefsHandler once
  • Save values with keys
  • Find values with keys
  • Delete value associated with a key
  • Save, Get and Delete List of Strings

How To Use

public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate() {
        super.onCreate();

	//To initialize and set data at the same time!
     new PrefsHandler.Builder(this)
     	// Optional (default: Context.MODE_PRIVATE)
        .setSharedPrefsMode(Context.MODE_PRIVATE)
        // Optional (default: "MySharedPreferences")
        .setSharedPrefsTag("MySharedPreferences")
        .build();

	//To get data
	String dataStr = PrefsHandler.getValue("key1", String.class);
	Integer dataInt = PrefsHandler.getValue("key2", Integer.class);
	SomeModel someMode = PrefsHandler.getValue("key3", SomeModel.class);

	//To delete data
	PrefsHandler.clearData("key");

	//To clear all data
	PrefsHandler.clearAll();
    }
}

To store different objects in shared preferences, use same syntax:

PrefsHandler
	.setValue("oneKey", myObject)
	.setValue("otherKey", myOtherObject)

Retrieving data can be as simple as:

DataType data = PrefsHandler.getValue("oneKey", DataType.class);
OtherDataType otherData =
PrefsHandler.getValue("otherKey", OtherDataType.class);

If data does not exist, getValue method will return null value.

You can also add list of objects to Shared Preferences with the same way:

// Creating Array list
ArrayList<String> myList = new ArrayList<>();

// Adding values
myList.add("First Strig");
myList.add("Second String");
myList.add("Third String");

// Saving list to the Shared Preferences
PrefsHandler.setValue("myList", myList);

// Getting list from Shared Preferences
List<String> listData = PrefsHandler.getListValue("myList");

Usage

For detailed usage, check the sampleApp above.

How to Setup

Step 1. Add it in your root build.gradle at the end of repositories:

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Step 2. Add the dependency

dependencies {
        compile 'com.github.BiAtoms:PrefsHandler:v1.2'
}

License

MIT


GitHub BiAtoms  ·