android - How to save a string globally? -


i want ask if possible store string globally me call in other activity? example string email in code, want save globally can call other activity.

i tried using intent carry data not seem work code.

private void checklogin(final string email, final string password) {     // tag used cancel request     string tag_string_req = "req_login";      pdialog.setmessage("logging in ...");     showdialog();      stringrequest strreq = new stringrequest(method.post,             appconfig.url_register, new response.listener<string>() {                  @override                 public void onresponse(string response) {                     log.d(tag, "login response: " + response.tostring());                     hidedialog();                      try {                         jsonobject jobj = new jsonobject(response);                         boolean error = jobj.getboolean("error");                          // check error node in json                         if (!error) {                             // user logged in                             // create login session                             session.setlogin(true);                              // launch main activity                             intent intent = new intent(loginactivity.this,                                     mainactivity.class);                                 startactivity(intent);                             finish();                         } else {                             // error in login. error message                             string errormsg = jobj.getstring("error_msg");                             toast.maketext(getapplicationcontext(),                                     errormsg, toast.length_long).show();                         }                     } catch (jsonexception e) {                         // json error                         e.printstacktrace();                     }                  }             }, new response.errorlistener() {                  @override                 public void onerrorresponse(volleyerror error) {                     log.e(tag, "login error: " + error.getmessage());                     toast.maketext(getapplicationcontext(),                             error.getmessage(), toast.length_long).show();                     hidedialog();                 }             }) {          @override         protected map<string, string> getparams() {             // posting parameters login url             map<string, string> params = new hashmap<string, string>();             params.put("tag", "login");             params.put("email", email);             params.put("password", password);              return params;         }      };      // adding request request queue     appcontroller.getinstance().addtorequestqueue(strreq, tag_string_req); } 

you can use sharedpreferences save data preference , use activity.

you can use method save email string sharedpreferences.

public void savevaluetoprefrence(context mcontext, string key, string value) {     sharedpreferences pref = mcontext.getsharedpreferences("userdata", 0);     sharedpreferences.editor editor = pref.edit();     editor.putstring(key, value);     editor.apply(); } 

you can email string in other activity using below method:

public string getvaluefromprefrence(context mcontext, string key) {     sharedpreferences pref = mcontext.getsharedpreferences("userdata", 0);     return pref.getstring(key, ""); } 

you can use method save email string:

savevaluetoprefrence(activityname.this,"email",email) 

you can email string this:

string email = getvaluefromprefrence(activityname.this,"email") 

basically need activity's context save , value sharedpreferences.

i hope helps you.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -