Pass data between activities in a single android app -
i have situation stated below:
__> b(setup page)------------> d(account settings) *all users have / | same layout / (has 10 rows/users) / (click 1 of account/row, highlight) / (once row selected, , click done, data saved in d / jumped , passed c. a(welcome page) \ \__> c(remote page)
above illustration of overall program~
i not know how pass data d through b c~ can guide me? code in c incomplete because has other function , class not related it.
code:
// code b: public class accountsetup extends activity { tablerow tablerow1, tablerow2, tablerow3, tablerow4, tablerow5, tablerow6, tablerow7, tablerow8, tablerow9, tablerow10; view border1, border2, border3, border4, border5, border6, border7, border8, border9, border10; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.account_main); this.initviews(); button btnadd = (button)findviewbyid(r.id.btnadd); btnadd.setonclicklistener(new onclicklistener() { public void onclick(view v) { if (tablerow1.getvisibility() == (view.gone)){ tablerow1.setvisibility(view.visible); border1.setvisibility(view.visible);} else if (tablerow2.getvisibility() == (view.gone)){ tablerow2.setvisibility(view.visible); border2.setvisibility(view.visible);} else if (tablerow3.getvisibility() == (view.gone)){ tablerow3.setvisibility(view.visible); border3.setvisibility(view.visible);} else if (tablerow4.getvisibility() == (view.gone)){ tablerow4.setvisibility(view.visible); border4.setvisibility(view.visible);} else if (tablerow5.getvisibility() == (view.gone)){ tablerow5.setvisibility(view.visible); border5.setvisibility(view.visible);} else if (tablerow6.getvisibility() == (view.gone)){ tablerow6.setvisibility(view.visible); border6.setvisibility(view.visible);} else if (tablerow7.getvisibility() == (view.gone)){ tablerow7.setvisibility(view.visible); border7.setvisibility(view.visible);} else if (tablerow8.getvisibility() == (view.gone)){ tablerow8.setvisibility(view.visible); border8.setvisibility(view.visible);} else if (tablerow9.getvisibility() == (view.gone)){ tablerow9.setvisibility(view.visible); border9.setvisibility(view.visible);} else if (tablerow10.getvisibility() == (view.gone)){ tablerow10.setvisibility(view.visible); border10.setvisibility(view.visible);} else{ final alertdialog alertdialog = new alertdialog.builder(accountsetup.this).create(); alertdialog.settitle("alert message"); alertdialog.setmessage("you have reached maximum number of users"); alertdialog.seticon(r.drawable.icon_tick); alertdialog.setbutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { //toast.maketext(getapplicationcontext(), "you clicked on ok", toast.length_short).show(); } }); alertdialog.show(); } }}); } private void initviews(){ //account 1 tablerow1 = (tablerow)findviewbyid(r.id.tablerow1); border1 = (view)findviewbyid(r.id.border1); tablerow1.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { tablerow1.setvisibility(view.gone); border1.setvisibility(view.gone); return true; } }); tablerow1.setonclicklistener(new onclicklistener() { public void onclick(view v) { tablerow1.setenabled(true); tablerow1.setselected(true); } }); button iconnext1 = (button)findviewbyid(r.id.iconnext1); iconnext1.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent1 = new intent(accountsetup.this, accountsettingactivity1.class); intent1.putextra("rowid","1"); startactivityforresult(intent1, 100); } }); //account 2 tablerow2 = (tablerow)findviewbyid(r.id.tablerow2); border2 = (view)findviewbyid(r.id.border2); tablerow2.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { tablerow2.setvisibility(view.gone); border2.setvisibility(view.gone); return true; } }); tablerow2.setonclicklistener(new onclicklistener() { public void onclick(view v) { tablerow2.setenabled(true); } }); button iconnext2 = (button)findviewbyid(r.id.iconnext2); iconnext2.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent2 = new intent(accountsetup.this, accountsettingactivity2.class); intent2.putextra("rowid","2"); startactivityforresult(intent2, 200); } }); //account 3 tablerow3 = (tablerow)findviewbyid(r.id.tablerow3); border3 = (view)findviewbyid(r.id.border3); tablerow3.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { tablerow3.setvisibility(view.gone); border3.setvisibility(view.gone); return true; } }); button iconnext3 = (button)findviewbyid(r.id.iconnext3); iconnext3.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent3 = new intent(accountsetup.this, accountsettingactivity3.class); intent3.putextra("rowid","3"); startactivityforresult(intent3, 300); } }); //account 4 tablerow4 = (tablerow)findviewbyid(r.id.tablerow4); border4 = (view)findviewbyid(r.id.border4); tablerow4.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { tablerow4.setvisibility(view.gone); border4.setvisibility(view.gone); return true; } }); button iconnext4 = (button)findviewbyid(r.id.iconnext4); iconnext4.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent4 = new intent(accountsetup.this, accountsettingactivity4.class); intent4.putextra("rowid","4"); startactivityforresult(intent4, 400); } }); //account 5 tablerow5 = (tablerow)findviewbyid(r.id.tablerow5); border5 = (view)findviewbyid(r.id.border5); tablerow5.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { tablerow5.setvisibility(view.gone); border5.setvisibility(view.gone); return true; } }); button iconnext5 = (button)findviewbyid(r.id.iconnext5); iconnext5.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent5 = new intent(accountsetup.this, accountsettingactivity5.class); intent5.putextra("rowid","5"); startactivityforresult(intent5, 500); } }); //account 6 tablerow6 = (tablerow)findviewbyid(r.id.tablerow6); border6 = (view)findviewbyid(r.id.border6); tablerow6.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { tablerow6.setvisibility(view.gone); border6.setvisibility(view.gone); return true; } }); button iconnext6 = (button)findviewbyid(r.id.iconnext6); iconnext6.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent6 = new intent(accountsetup.this, accountsettingactivity6.class); intent6.putextra("rowid","6"); startactivityforresult(intent6, 600); } }); //account 7 tablerow7 = (tablerow)findviewbyid(r.id.tablerow7); border7 = (view)findviewbyid(r.id.border7); tablerow7.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { tablerow7.setvisibility(view.gone); border7.setvisibility(view.gone); return true; } }); button iconnext7 = (button)findviewbyid(r.id.iconnext7); iconnext7.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent7 = new intent(accountsetup.this, accountsettingactivity7.class); intent7.putextra("rowid","7"); startactivityforresult(intent7, 700); } }); //account 8 tablerow8 = (tablerow)findviewbyid(r.id.tablerow8); border8 = (view)findviewbyid(r.id.border8); tablerow8.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { tablerow8.setvisibility(view.gone); border8.setvisibility(view.gone); return true; } }); button iconnext8 = (button)findviewbyid(r.id.iconnext8); iconnext8.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent8 = new intent(accountsetup.this, accountsettingactivity8.class); intent8.putextra("rowid","8"); startactivityforresult(intent8, 800); } }); //account 9 tablerow9 = (tablerow)findviewbyid(r.id.tablerow9); border9 = (view)findviewbyid(r.id.border9); tablerow9.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { tablerow9.setvisibility(view.gone); border9.setvisibility(view.gone); return true; } }); button iconnext9 = (button)findviewbyid(r.id.iconnext9); iconnext9.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent9 = new intent(accountsetup.this, accountsettingactivity9.class); intent9.putextra("rowid","9"); startactivityforresult(intent9, 900); } }); //account 10 tablerow10 = (tablerow)findviewbyid(r.id.tablerow10); border10 = (view)findviewbyid(r.id.border10); tablerow10.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { tablerow10.setvisibility(view.gone); border10.setvisibility(view.gone); return true; } }); button iconnext10 = (button)findviewbyid(r.id.iconnext10); iconnext10.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent10 = new intent(accountsetup.this, accountsettingactivity10.class); intent10.putextra("rowid","10"); startactivityforresult(intent10, 1000); } }); } public void onbackpressed() { intent = new intent(accountsetup.this, welcomeactivity.class); startactivity(i); finish(); super.onbackpressed(); } @override protected void onactivityresult(int requestcode,int resultcode, intent data) { sharedpreferences sp = preferencemanager.getdefaultsharedpreferences(this); if (requestcode == 100) { if (resultcode == result_ok) { data.getstringextra("accountname1"); textview user1 = (textview)findviewbyid(r.id.user1); user1.settext(sp.getstring("accountname1", "")); } } else if (requestcode == 200) { if (resultcode == result_ok) { data.getstringextra("accountname2"); textview user2 = (textview)findviewbyid(r.id.user2); user2.settext(sp.getstring("accountname2", "")); } } else if (requestcode == 300) { if (resultcode == result_ok) { data.getstringextra("accountname3"); textview user3 = (textview)findviewbyid(r.id.user3); user3.settext(sp.getstring("accountname3", "")); } } else if (requestcode == 400) { if (resultcode == result_ok) { data.getstringextra("accountname4"); textview user4 = (textview)findviewbyid(r.id.user4); user4.settext(sp.getstring("accountname4", "")); } } else if (requestcode == 500) { if (resultcode == result_ok) { data.getstringextra("accountname5"); textview user5 = (textview)findviewbyid(r.id.user5); user5.settext(sp.getstring("accountname5", "")); } } else if (requestcode == 600) { if (resultcode == result_ok) { data.getstringextra("accountname6"); textview user6 = (textview)findviewbyid(r.id.user6); user6.settext(sp.getstring("accountname6", "")); } } else if (requestcode == 700) { if (resultcode == result_ok) { data.getstringextra("accountname7"); textview user7 = (textview)findviewbyid(r.id.user7); user7.settext(sp.getstring("accountname7", "")); } } else if (requestcode == 800) { if (resultcode == result_ok) { data.getstringextra("accountname8"); textview user8 = (textview)findviewbyid(r.id.user8); user8.settext(sp.getstring("accountname8", "")); } } else if (requestcode == 900) { if (resultcode == result_ok) { data.getstringextra("accountname9"); textview user9 = (textview)findviewbyid(r.id.user9); user9.settext(sp.getstring("accountname9", "")); } } else if (requestcode == 1000) { if (resultcode == result_ok) { data.getstringextra("accountname10"); textview user10 = (textview)findviewbyid(r.id.user10); user10.settext(sp.getstring("accountname10", "")); } } } @override public void onresume() { super.onresume(); sharedpreferences sp = preferencemanager.getdefaultsharedpreferences(this); textview user1 = (textview)findviewbyid(r.id.user1); user1.settext(sp.getstring("accountname1", "")); textview user2 = (textview)findviewbyid(r.id.user2); user2.settext(sp.getstring("accountname2", "")); textview user3 = (textview)findviewbyid(r.id.user3); user3.settext(sp.getstring("accountname3", "")); textview user4 = (textview)findviewbyid(r.id.user4); user4.settext(sp.getstring("accountname4", "")); textview user5 = (textview)findviewbyid(r.id.user5); user5.settext(sp.getstring("accountname5", "")); textview user6 = (textview)findviewbyid(r.id.user6); user6.settext(sp.getstring("accountname6", "")); textview user7 = (textview)findviewbyid(r.id.user7); user7.settext(sp.getstring("accountname7", "")); textview user8 = (textview)findviewbyid(r.id.user8); user8.settext(sp.getstring("accountname8", "")); textview user9 = (textview)findviewbyid(r.id.user9); user9.settext(sp.getstring("accountname9", "")); textview user10 = (textview)findviewbyid(r.id.user10); user10.settext(sp.getstring("accountname10", "")); } } // code d: ---------------------------------------------------------- public class accountsettingactivity1 extends activity{ private edittext etaccountname; private edittext etwanip; private edittext etlocalip; private edittext etport; private edittext etpassword; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.account_tab_content_setting); this.initviews(); } private void initviews(){ etaccountname = (edittext)this.findviewbyid(r.id.etaccountname); etwanip = (edittext)this.findviewbyid(r.id.etwanip); etlocalip = (edittext)this.findviewbyid(r.id.etlocalip); etport = (edittext)this.findviewbyid(r.id.etport); etpassword = (edittext)this.findviewbyid(r.id.etpassword); // assigns value sharedpreferences sp = preferencemanager.getdefaultsharedpreferences(this); etaccountname.settext(sp.getstring("accountname1", "")); etwanip.settext(sp.getstring("wanip1", "")); etlocalip.settext(sp.getstring("localip1", "")); etport.settext(sp.getstring("port1", "")); etpassword.settext(sp.getstring("password1", "")); etwanip.setonfocuschangelistener(new onfocuschangelistener(){ @override public void onfocuschange(view arg0, boolean hasfocus) { if(!hasfocus){ system.out.println("lost focus"); accountsettingactivity1.this.savesettings(); } } }); } private void savesettings(){ string accountname1 = etaccountname.gettext().tostring(); string wanip1 = etwanip.gettext().tostring(); string localip1 = etlocalip.gettext().tostring(); string port1 = etport.gettext().tostring(); string password1 = etpassword.gettext().tostring(); accountname1 = (accountname1.trim().length() == 0)? "user": accountname1; wanip1 = (wanip1.trim().length() == 0)? "0.0.0.0": wanip1; localip1 = (localip1.trim().length() == 0)? "0.0.0.0": localip1; port1 = (port1.trim().length() == 0)? "8000": port1; password1 = (password1.trim().length() == 0)? "xxxx": password1; etaccountname.settext(accountname1); etwanip.settext(wanip1); etlocalip.settext(localip1); etport.settext(port1); etpassword.settext(password1); sharedpreferences.editor editor = preferencemanager .getdefaultsharedpreferences(this).edit(); editor.putstring("accountname1", etaccountname.gettext().tostring()); editor.putstring("wanip1", etwanip.gettext().tostring()); editor.putstring("localip1", etlocalip.gettext().tostring()); editor.putstring("port1", etport.gettext().tostring()); editor.putstring("password1", etpassword.gettext().tostring()); editor.commit(); } public void onbackpressed() { savesettings(); etaccountname = (edittext)findviewbyid(r.id.etaccountname); etwanip = (edittext)findviewbyid(r.id.etwanip); etlocalip = (edittext)findviewbyid(r.id.etlocalip); etport = (edittext)findviewbyid(r.id.etport); etpassword = (edittext)findviewbyid(r.id.etpassword); string accountname = etaccountname.gettext().tostring(); string wanip = etwanip.gettext().tostring(); string localip = etlocalip.gettext().tostring(); string port = etport.gettext().tostring(); string password = etpassword.gettext().tostring(); intent = new intent(accountsettingactivity1.this, accountsetup.class); i.putextra("accountname1" ,accountname); i.putextra("wanip1" ,wanip); i.putextra("localip1" ,localip); i.putextra("port1" ,port); i.putextra("password1" ,password); setresult(result_ok, i); finish(); super.onbackpressed(); } @override protected void onpause() { // when user leaves tab, saves values this.savesettings(); super.onpause(); } } // code c: -------------------------------------------------------------- public class settingactivity extends activity implements toolbarcallback { private toolbar toolbar; private edittext etaccountname; private edittext etwanip; private edittext etlocalip; private edittext etport; private edittext etpassword; private button btnconnection; private viewswitcher switcher; private relativelayout infoview; private bluguardproto2activity mainactivity; private bluguardprotocol bluguard; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.tab_content_setting); this.initviews(); bluguard = bluguardprotocol.getinstance(); } @override protected void onresume() { super.onresume(); this.toolbar.setprotocolcallback(this); this.toolbar.setrightbuttondrawable(r.drawable.ic_btn_info); this.toolbar.setrightbuttononclicklistener(new onclicklistener(){ @override public void onclick(view arg0) { showinfoview(); } }); this.toolbar.setleftbuttononclicklistener(new onclicklistener(){ @override public void onclick(view arg0) { savesettings(); if(bluguard.isloggedinandconnected()){ toolbar.disconnect(); } else { toolbar.connect(); } } }); if(bluguard.isloggedinandconnected()){ btnconnection.settext(getresources().getstring(r.string.disconnect)); } else { btnconnection.settext(getresources().getstring(r.string.connect)); } } private void initviews(){ mainactivity = (bluguardproto2activity) this.getparent(); toolbar = (toolbar)mainactivity.findviewbyid(r.id.toolbar); etaccountname = (edittext)this.findviewbyid(r.id.etaccountname); etwanip = (edittext)this.findviewbyid(r.id.etwanip); etlocalip = (edittext)this.findviewbyid(r.id.etlocalip); etport = (edittext)this.findviewbyid(r.id.etport); etpassword = (edittext)this.findviewbyid(r.id.etpassword); switcher = (viewswitcher)this.findviewbyid(r.id.switcher); infoview = (relativelayout)this.findviewbyid(r.id.infoview); btnconnection = (button)this.findviewbyid(r.id.btnconnection); btnconnection.setonclicklistener(new onclicklistener(){ @override public void onclick(view arg0) { if(bluguard.isloggedinandconnected()){ toolbar.disconnect(); btnconnection.settext(getresources().getstring(r.string.connect)); } else { savesettings(); toolbar.connect(); } } }); // asigns value sharedpreferences sp = preferencemanager.getdefaultsharedpreferences(this); etaccountname.settext(sp.getstring("accountname", "")); etwanip.settext(sp.getstring("wanip", "")); etlocalip.settext(sp.getstring("localip", "")); etport.settext(sp.getstring("port", "")); etpassword.settext(sp.getstring("password", "")); etwanip.setonfocuschangelistener(new onfocuschangelistener(){ @override public void onfocuschange(view arg0, boolean hasfocus) { if(!hasfocus){ system.out.println("lost focus"); settingactivity.this.savesettings(); } } }); } private void showinfoview(){ mainactivity.hidetabs(); this.toolbar.setrightbuttondrawable(0); this.toolbar.setrightbuttononclicklistener(null); this.toolbar.setleftbuttondrawable(r.drawable.ic_btn_back); this.toolbar.setleftbuttononclicklistener(new onclicklistener(){ @override public void onclick(view v) { showsettingsview(); } }); this.toolbar.settext(getresources().getstring(r.string.about)); switcher.shownext(); } private void showsettingsview(){ this.toolbar.initconnectionui(); this.toolbar.setrightbuttondrawable(r.drawable.ic_btn_info); this.toolbar.setrightbuttononclicklistener(new onclicklistener(){ @override public void onclick(view arg0) { showinfoview(); } }); mainactivity.showtabs(); switcher.showprevious(); } @override protected void onpause() { // when user leaves tab, saves values super.onpause(); } @override public void onbackpressed() { intent = new intent(settingactivity.this, welcomeactivity.class); startactivity(i); finish(); super.onbackpressed(); } } // code a: -------------------------------------------------------------- public class welcomeactivity extends activity { private boolean firstrun; private long lastbackpresstime; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.setcontentview(r.layout.welcome); sharedpreferences sp = preferencemanager.getdefaultsharedpreferences(this); if(sp.getboolean("firstrun", true)){ preferencemanager.setdefaultvalues(this, r.xml.preferences, true); firstrun = true; sharedpreferences.editor editor = sp.edit(); editor.putboolean("firstrun", false); editor.commit(); } else { firstrun = false; } this.initviews(); } private void initviews(){ button btncontinue = (button)this.findviewbyid(r.id.btncontinue); btncontinue.setonclicklistener(new onclicklistener(){ //@override public void onclick(view v) { intent = new intent(welcomeactivity.this, bluguardproto2activity.class); // i.setflags(intent.flag_activity_no_history); i.putextra("firstrun", firstrun); startactivity(i); } }); button btnaccount = (button)findviewbyid(r.id.btnaccount); btnaccount.setonclicklistener(new onclicklistener() { //@override public void onclick(view v) { intent h = new intent(welcomeactivity.this,accountsetup.class); startactivity(h); }}); } @override public void onbackpressed() { if (this.lastbackpresstime < system.currenttimemillis() - 5000) { toast.maketext(this, "press again close app",5000).show(); this.lastbackpresstime = system.currenttimemillis(); } else { super.onbackpressed(); } } }
from activity
check code
intent productsintent = new intent(this, productactivity.class); bundle param = new bundle(); param.putint("categories", 0); //your id param.putcharsequence("categoryname", "no category"); productsintent.putextras(param);
target activity
bundle b = getintent().getextras(); int id = b.getint("categories");
Comments
Post a Comment