android - Global variables not passing properly -
i have created class holds common variables , functions , inherited activity classes interface different ui pages in app. have been passing information between classes , activities using getvariable() , setvariable(input) functions. suddenly, can no longer pass information way (it had been working until recent edits, , can't figure out change screwed up). have used log outputs determine data storing - setvariable(input) functions - when called later getvariable() functions returns null. thoughts?
*note, started incorporating fragments project, extending fragmentactivity instead of activity on main class. don't think causing problem, it? if does, whats best practice pass global variable info, , use fragments?
code samples:
main inherited class:
public class menubaractivity extends fragmentactivity { private string keya; private string keyb; private int token; private string salt; private long expires; public string getkeyb() { return keyb; } public string getkeya() { return keya; } public int gettokenid() { return token; } public void settoken(int tkn) { token = tkn; } public void setkeyb(string kyb) { keyb = kyb; } public void setkeya(string kya) { keya = kya; } //other common functions } login activity class (gets log in info web, stores global variables):
public class webcontentget extends menubaractivity{ public int trylogon(string uemail, string pw) { //call new keys on start jsonobject jobsend = new jsonobject(); try { jobsend.put("email", uemail); jobsend.put("password", pw); t.start(); t.join(); if(getstatus() == user_status_successfullogin){ string data = getdata(); jsonobject jobreturn = new jsonobject(data); string kya = jobreturn.getstring("keya"); string kyb = jobreturn.getstring("keyb"); int tkn = integer.parseint(jobreturn.getstring("tokenid")); string salt = jobreturn.getstring("salt"); long exp = long.parselong(jobreturn.getstring("expiration")); int uid = integer.parseint(jobreturn.getstring("userid")); // log outputs confirm data being read properly, , reported setx() functions settoken(tkn); setkeya(kya); setkeyb(kyb); setsalt(salt); setexpires(exp); log.d("webcontentget trylogin","login values stored"); } return getstatus(); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); return getstatus(); } } activity class, checks if keya/b/etc stored:
public class userlogin2 extends menubaractivity implements emaillistener { string emailin; string pwin; context context = this; @override public void onemailloginclick(string email, string pw) { log.d("userlogin2", "onemailloginclick"); emailin = email; pwin = pw; emailin = emailin.trim(); emailin = emailin.touppercase(); log.d("prepped email", emailin); pwin = pwin.trim(); webcontentget webob = new webcontentget(); int weblog = webob.trylogon(emailin, pwin); if (weblog == user_status_successfullogin) { int tkn = gettokenid(); long exp = getexpires(); string kya = getkeya(); string kyb = getkeyb(); string slt = getsalt(); log.d("userlogin2 - token", string.valueof(tkn)); //log statements confirm getx() functions returning null session.storeloginsession(emailin, pwin, thisuser, tkn, exp, kya, kyb, slt); intent intent1 = new intent(context, mainactivitiy.class); startactivity(intent1); } else { showdialog(this, "log in failure", "incorrect password"); } } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.userlogin2); } }
this cannot work, because have 2 differend instances of menubaractivity. not way pass data 1 activity in android.
if want use data 1 activity in activity, have add them intent in activity provides data, , extract them in other. more information see here: how pass data between activities in android application?
if don't want start activity , send data intent, have store data somewhere e.g. sharedpreferences , fetch them again: how use sharedpreferences in android store, fetch , edit values
Comments
Post a Comment