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

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -