java - Android Object Serializes itself from method -
i need , object containing settings in primitive types able load , save , android shared preferences calling load or save method object.
i have created class called thepreferences.java, class has few primitive fields. have created 2x methods within class load , save class , sharedpreferences. see below code used in class:
import android.content.context; import android.content.sharedpreferences; import com.google.gson.gson; import com.google.gson.jsonserializer; import android.content.sharedpreferences; import android.support.v7.app.appcompatactivity; import android.util.log; import java.io.serializable; public class thepreferences extends appcompatactivity implements serializable { private static final string prefs_name = "thepreferences"; private static final string pref_text = "pref_text"; public static final string tag = "wpc"; public boolean loggedin = false; public int deviceid = 0; public int userid = 0; public string email = ""; public string devicealias = ""; public thepreferences() { } public thepreferences(boolean loggedin, int deviceid, int userid, string email, string devicealias) { this.loggedin = loggedin; this.deviceid = deviceid; this.userid = userid; this.email = email; this.devicealias = devicealias; } public boolean loadthepreferences(context c) { sharedpreferences pref = c.getsharedpreferences(prefs_name, mode_private); if (pref.contains(pref_text)) { string thejson = pref.getstring(pref_text, null); log.i(tag, "the json string loaded shared preferences : " + thejson); thepreferences p = new gson().fromjson(thejson, thepreferences.class); this.devicealias = (p.devicealias); this.deviceid = (p.deviceid); this.email = (p.email); this.loggedin = (p.loggedin); return true; } else { log.i(tag, "could not load sharedpreferences, value not exist."); return false; } } public static boolean savethepreferences(context c,thepreferences p){ gson gson = new gson(); string thejson = gson.tojson(p); c.getsharedpreferences(prefs_name,mode_private) .edit() .putstring(pref_text,thejson) .commit(); return true; } @override public string tostring() { return "thepreferences{" + "loggedin=" + this.loggedin + ", deviceid=" + this.deviceid + ", userid=" + this.userid + ", email='" + this.email + '\'' + ", devicealias='" + this.devicealias + '\'' + '}'; } }
i try call activity load , save preferences follow:
contextofapplication = getapplicationcontext(); thepreferences preferencesobj = new thepreferences(true,12345,552441,"abc@123.com","thedeivcealias"); preferencesobj.savethepreferences(contextofapplication,preferencesobj); preferencesobj.loadthepreferences(contextofapplication);
i cannot execute past savethepreferences() method. keep on crashin stackoverflow exeption , cannot figure out why.'
can please advise if there way of doing or maybe have gone wrong?
thank in advance
passing prefernecesobj itself
preferencesobj.savethepreferences(contextofapplication,preferencesobj);
change function to:
public boolean savethepreferences(context c){ gson gson = new gson(); string thejson = gson.tojson(this); c.getsharedpreferences(prefs_name,mode_private) .edit() .putstring(pref_text,thejson) .commit(); return true; }
then call this:
preferencesobj.savethepreferences(contextofapplication);
Comments
Post a Comment