Skip to content
Hendra Permana edited this page May 6, 2017 · 3 revisions

Take a look a full example here : https://github.com/hynra/GSON-SharedPreferences/blob/master/example.md

Add to your Project

  • on your build.gradle root :
	allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}
  • Add dependency
	dependencies {
  	compile 'com.github.hynra:GSON-SharedPreferences:1.1'
  }

Create Object

Prepare your (Gson) Object via generator. For example, you can generate your obect from JSON Object to (Gson) object with online tool like http://www.jsonschema2pojo.org/, then add generated object(s) to your project.

Init!

With Context Only

GSONSharedPreferences gsonSharedPreferences;
context = this;
gsonSharedPreferences = new GSONSharedPreferences(context);

it will save in PreferenceManager.getDefaultSharedPreferences().

With Context & Preferences name

GSONSharedPreferences gsonSharedPreferences;
context = this;
String prefName = "whatever"
gsonSharedPreferences = new GSONSharedPreferences(context, prefName);

it will use Context.MODE_PRIVATE mode.

With Context, Preferences name & mode.

GSONSharedPreferences gsonSharedPreferences;
context = this;
String prefName = "whatever"
int mode = Context.MODE_PRIVATE;
gsonSharedPreferences = new GSONSharedPreferences(context, prefName, mode);