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

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -