c# - Save the state of my Activity xamarin.android -
i want save state of activity when closes app. contains listactivity simplelistitemchecked...
namespace xamarinscanner { [activity(label = "@string/scanhistory", screenorientation = screenorientation.portrait)] public class scanhistoryactivity : listactivity { protected override void oncreate(bundle bundle) { base.oncreate(bundle); var codes = intent.extras.getstringarraylist("codes"); codes.tolist(); listadapter = new arrayadapter<string>(this, android.resource.layout.simplelistitemchecked, codes); listview lv = findviewbyid<listview>(android.resource.id.list); lv.choicemode = choicemode.multiple; foreach (var c in codes) { if (c.contains("success")) { int position = codes.indexof(c); lv.setitemchecked(position, true); } } } }
}
this save state mainactivity, seems keep data listactivity , main activity when backgrounded. think need same thing when app destroyed..
protected override void onsaveinstancestate(bundle outstate)' { outstate.putstringarraylist("codes", _codes.toarray()); base.onsaveinstancestate(outstate); } protected override void oncreate(bundle bundle) { url = "http://10.0.0.103:4321/scan"; if (bundle != null) { var c = bundle.getstringarraylist("codes"); instance state"); } base.oncreate(bundle); setcontentview(resource.layout.main);`
what mean when user close app? mean when close moment , open again or when app closed completely?
if mean 2nd, should use ondestroy "save" state , load in on oncreate in next time. have manually create save , load procedure (with database?sharedpreferences? it's you)
check here activity lifecycle.
also post have try far , didnt want , exact results expecting
a simple way of storing value on ondestroy sharedpreferences:
isharedpreferences prefs = preferencemanager.getdefaultsharedpreferences (application.context); isharedpreferenceseditor editor = prefs.edit (); editor.putboolean ("keyname", _bool); // editor.commit(); // applies changes synchronously on older apis editor.apply(); // applies changes asynchronously on newer apis
access saved values using:
isharedpreferences prefs = preferencemanager.getdefaultsharedpreferences (application.context); _bool = prefs.getboolean ("keyname"); _int = prefs.getint ("keyname"); _string = prefs.getstring ("keyname");
Comments
Post a Comment