Sunday, 15 January 2012

Save custom objects when activty stops on Android -



Save custom objects when activty stops on Android -

i've written little andorid app. app uses vector of custom objects , displays them in listview. want save objects when activity send background. best way this. in vector 25 objects. every object has boolean, 2 long, , 2 strings.

thanks help

do need them saved when app shuts downwards or when activity goes background?

if objects parcelables utilize save , restore instance state methods:

@override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putparcelablearraylist("objectsarray", myobjects); // if array of parceleble objects } @override protected void onrestoreinstancestate(bundle savedinstancestate) { super.onrestoreinstancestate(savedinstancestate); myobjects= savedinstancestate.getparcelablearraylist("objectsarray"); }

here illustration of parcelable object

public class knusersketch implements parcelable{

public int id; public int user; public string url; public int views; public int locations; public knusersketch(jsonobject obj) { id = knsafejsonutilties.safegetinteger(obj, "id"); user = knsafejsonutilties.safegetinteger(obj, "user"); views = knsafejsonutilties.safegetinteger(obj, "views"); locations = knsafejsonutilties.safegetinteger(obj, "locations"); url = knsafejsonutilties.safegetstring(obj, "url"); log.v("json","jsonobject: "+obj.tostring()); url.replace("https:", "http:"); } public knusersketch(){ id=-1; user=-1; views = 0; url =""; } public knusersketch(parcel p){ id= p.readint(); user = p.readint(); url = p.readstring(); views = p.readint(); locations = p.readint(); } @override public int describecontents() { // todo auto-generated method stub homecoming 0; } @override public void writetoparcel(parcel dest, int flags) { // todo auto-generated method stub dest.writeint(id); dest.writeint(user); dest.writestring(url); dest.writeint(views); dest.writeint(locations); } public static final parcelable.creator<knusersketch> creator = new creator<knusersketch>() { public knusersketch createfromparcel(parcel source) { homecoming new knusersketch(source); } public knusersketch[] newarray(int size) { homecoming new knusersketch[size]; } };

}

android android-activity storage

No comments:

Post a Comment