Android using variable for first time -



Android using variable for first time -

i need set values associated persistently stored data, if exist, on initialization. if not need initialize them. there disadvantage using sharedpreference initialize variable on first run. is, :

@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); sharedpreferences sp = getsharedpreferences("mydataname", context.mode_private); string name = sp.getstring("name", ""); boolean isfirstrunning = sp.getboolean("firsttime", true); if (isfirstrunning) { toast.maketext(this, "yea", toast.length_long).show(); sharedpreferences.editor editor = sp.edit(); editor.putboolean("firsttime", false); editor.commit(); } }

if there no disadvantage processing level, there standard practice far situation concerned? also, there alternative way handle persistent data, or have utilize sharedpreferences this?

yes that's acceptable. neatness may improve define keys, , default values constants approach have work fine.

android android-sharedpreferences

Comments

Popular posts from this blog

maven fortify plugin : Unable to load build session with ID XXXXX .. See log file for more details -

c# - Primavera WebServices does not return any data -

android - Display emoji panel with genymotion - keyboard/touch input? -