java - shared preferenced across mutiple activities -
i have project need use shared preference. i'm trying shared preference @ moment, instead of passing string value through intent, i'm passing through shared preference. in first activity have this:
sharedpreferences prefs = this.getsharedpreferences("com.example.mayankthakur.personalprojecttrial2", context.mode_private); intent namesave = getintent(); name = namesave.getstringextra("name") prefs.edit().putstring(name, "name").apply();
in second activity
sharedpreferences preferences = getsharedpreferences("prefs", context.mode_private); name = preferences.getstring("name", name); example.settext(name);
for now, have textview in second activity show value of string name. app doesn't crash, there 1 bug. instead of showing value of string name, textview shows "name" without quotes. looked , turns out need string key string method , put string. know string key (so don't have problem later) , how can fix issue in code.
any appreciated
there 2 changes here..
prefs.edit().putstring("name", name).apply();
instead of writing name in getstring have pass default value,so change code this,
sharedpreferences preferences = getsharedpreferences("prefs", context.mode_private); example.settext(preferences.getstring("name",null);
this solve problem
Comments
Post a Comment