android - Identifying Which StartActivityForResult is calling the current Activity -
i have situation stated below.
activity can call b or d.
b calls c, after input details c, , press back, c pass data b. b, if select either row, row bring me d.
now, problem is, d, want set if-else statement know startactivityforresult b calling d, can send respective data c b , d.
_____>b------>c / | a----> |(b can call d , pass data selected row b d.) \_____>d
code activity d:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.tab_content_setting); this.initviews(); bluguard = bluguardprotocol.getinstance(); } 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 { toolbar.connect();}}}); // assigns value string choice = getintent().getstringextra("activity"); //bundle extras = intent.getextras(); sharedpreferences sp = preferencemanager.getdefaultsharedpreferences(this); if(choice != null && choice.equals("activity2")) { //if(extras.contains("activity2")){ etaccountname.settext(sp.getstring("accountname2", "")); etwanip.settext(sp.getstring("wanip2", "")); etlocalip.settext(sp.getstring("localip2", "")); etport.settext(sp.getstring("port2", "")); etpassword.settext(sp.getstring("password2", ""));} } @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) { 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 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() { super.onpause(); } @override public void onbackpressed() { intent = new intent(settingactivity.this, welcomeactivity.class); startactivity(i); finish(); super.onbackpressed(); } @override public void onlogincompleted(boolean succeeded, int code) { if(succeeded){ btnconnection.settext(getresources().getstring(r.string.disconnect)); } } @override public void ontogglealarmcompleted(boolean succeeded, int code) { // nothing } @override public void ontogglehacompleted(boolean succeeded, int code) { // nothing } @override public void onstatuspollcompleted(alarmstatus status) { // nothing} @override public void ondisconnected() { btnconnection.settext(getresources().getstring(r.string.connect));} @override public void onrequesttimedout(timeouttype type) { // nothing} @override public void onprereconnect() { inputmethodmanager imm = (inputmethodmanager)getsystemservice(context.input_method_service); imm.hidesoftinputfromwindow(this.getcurrentfocus().getwindowtoken(), 0);}}
code activity b:
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.account_main); this.initviews(); //set onclick on button add 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) { } }); alertdialog.show(); } }}); } private void initviews(){ //account 1 tablerow1 = (tablerow)findviewbyid(r.id.tablerow1); border1 = (view)findviewbyid(r.id.border1); iconnext1 = (button)findviewbyid(r.id.iconnext1); tablerow1.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { alertdialog.builder alertdialog = new alertdialog.builder(accountsetup.this); alertdialog.settitle("user account deletion"); alertdialog.setmessage("are sure delete user account?"); alertdialog.seticon(r.drawable.icon_alert); alertdialog.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int which) { tablerow1.setvisibility(view.gone); border1.setvisibility(view.gone);}}); alertdialog.setnegativebutton("no", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel();}}); alertdialog.show(); return true; }}); tablerow1.setonclicklistener(new onclicklistener() { public void onclick(view v) { /*tablerow1.setenabled(true); tablerow1.setselected(true); tablerow1.requestfocusfromtouch();*/ intent intent1 = new intent(accountsetup.this, bluguardproto2activity.class); intent1.putextra("activity", "activity1"); startactivityforresult(intent1, 150); } }); iconnext1.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent1 = new intent(accountsetup.this, accountsettingactivity1.class); startactivityforresult(intent1, 100);}}); //account 2 tablerow2 = (tablerow)findviewbyid(r.id.tablerow2); border2 = (view)findviewbyid(r.id.border2); iconnext2 = (button)findviewbyid(r.id.iconnext2); tablerow2.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { alertdialog.builder alertdialog = new alertdialog.builder(accountsetup.this); alertdialog.settitle("user account deletion"); alertdialog.setmessage("are sure delete user account?"); alertdialog.seticon(r.drawable.icon_alert); alertdialog.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int which) { tablerow2.setvisibility(view.gone); border2.setvisibility(view.gone);}}); alertdialog.setnegativebutton("no", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel();}}); alertdialog.show(); return true;}}); tablerow2.setonclicklistener(new onclicklistener() { public void onclick(view v) { /*tablerow1.setenabled(true); tablerow1.setselected(true); tablerow1.requestfocusfromtouch();*/ intent intent2 = new intent(accountsetup.this, bluguardproto2activity.class); intent2.putextra("activity", "activity2"); startactivityforresult(intent2, 250); } }); iconnext2.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent2 = new intent(accountsetup.this, accountsettingactivity2.class); startactivityforresult(intent2, 200);}}); //account 3 tablerow3 = (tablerow)findviewbyid(r.id.tablerow3); border3 = (view)findviewbyid(r.id.border3); iconnext3 = (button)findviewbyid(r.id.iconnext3); tablerow3.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { alertdialog.builder alertdialog = new alertdialog.builder(accountsetup.this); alertdialog.settitle("user account deletion"); alertdialog.setmessage("are sure delete user account?"); alertdialog.seticon(r.drawable.icon_alert); alertdialog.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int which) { tablerow3.setvisibility(view.gone); border3.setvisibility(view.gone);}}); alertdialog.setnegativebutton("no", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel();}}); alertdialog.show(); return true;}}); iconnext3.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent3 = new intent(accountsetup.this, accountsettingactivity3.class); startactivityforresult(intent3, 300);}}); //account 4 tablerow4 = (tablerow)findviewbyid(r.id.tablerow4); border4 = (view)findviewbyid(r.id.border4); iconnext4 = (button)findviewbyid(r.id.iconnext4); tablerow4.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { alertdialog.builder alertdialog = new alertdialog.builder(accountsetup.this); alertdialog.settitle("user account deletion"); alertdialog.setmessage("are sure delete user account?"); alertdialog.seticon(r.drawable.icon_alert); alertdialog.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int which) { tablerow4.setvisibility(view.gone); border4.setvisibility(view.gone);}}); alertdialog.setnegativebutton("no", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel();}}); alertdialog.show(); return true;}}); iconnext4.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent4 = new intent(accountsetup.this, accountsettingactivity4.class); startactivityforresult(intent4, 400);}}); //account 5 tablerow5 = (tablerow)findviewbyid(r.id.tablerow5); border5 = (view)findviewbyid(r.id.border5); iconnext5 = (button)findviewbyid(r.id.iconnext5); tablerow5.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { alertdialog.builder alertdialog = new alertdialog.builder(accountsetup.this); alertdialog.settitle("user account deletion"); alertdialog.setmessage("are sure delete user account?"); alertdialog.seticon(r.drawable.icon_alert); alertdialog.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int which) { tablerow5.setvisibility(view.gone); border5.setvisibility(view.gone);}}); alertdialog.setnegativebutton("no", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel();}}); alertdialog.show(); return true;}}); iconnext5.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent5 = new intent(accountsetup.this, accountsettingactivity5.class); startactivityforresult(intent5, 500);}}); //account 6 tablerow6 = (tablerow)findviewbyid(r.id.tablerow6); border6 = (view)findviewbyid(r.id.border6); iconnext6 = (button)findviewbyid(r.id.iconnext6); tablerow6.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { alertdialog.builder alertdialog = new alertdialog.builder(accountsetup.this); alertdialog.settitle("user account deletion"); alertdialog.setmessage("are sure delete user account?"); alertdialog.seticon(r.drawable.icon_alert); alertdialog.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int which) { tablerow6.setvisibility(view.gone); border6.setvisibility(view.gone);}}); alertdialog.setnegativebutton("no", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel();}}); alertdialog.show(); return true;}}); iconnext6.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent6 = new intent(accountsetup.this, accountsettingactivity6.class); startactivityforresult(intent6, 600);}}); //account 7 tablerow7 = (tablerow)findviewbyid(r.id.tablerow7); border7 = (view)findviewbyid(r.id.border7); iconnext7 = (button)findviewbyid(r.id.iconnext7); tablerow7.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { alertdialog.builder alertdialog = new alertdialog.builder(accountsetup.this); alertdialog.settitle("user account deletion"); alertdialog.setmessage("are sure delete user account?"); alertdialog.seticon(r.drawable.icon_alert); alertdialog.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int which) { tablerow7.setvisibility(view.gone); border7.setvisibility(view.gone);}}); alertdialog.setnegativebutton("no", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel();}}); alertdialog.show(); return true;}}); iconnext7.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent7 = new intent(accountsetup.this, accountsettingactivity7.class); startactivityforresult(intent7, 700);}}); //account 8 tablerow8 = (tablerow)findviewbyid(r.id.tablerow8); border8 = (view)findviewbyid(r.id.border8); iconnext8 = (button)findviewbyid(r.id.iconnext8); tablerow8.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { alertdialog.builder alertdialog = new alertdialog.builder(accountsetup.this); alertdialog.settitle("user account deletion"); alertdialog.setmessage("are sure delete user account?"); alertdialog.seticon(r.drawable.icon_alert); alertdialog.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int which) { tablerow8.setvisibility(view.gone); border8.setvisibility(view.gone);}}); alertdialog.setnegativebutton("no", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel();}}); alertdialog.show(); return true;}}); iconnext8.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent8 = new intent(accountsetup.this, accountsettingactivity8.class); startactivityforresult(intent8, 800);}}); //account 9 tablerow9 = (tablerow)findviewbyid(r.id.tablerow9); border9 = (view)findviewbyid(r.id.border9); iconnext9 = (button)findviewbyid(r.id.iconnext9); tablerow9.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { alertdialog.builder alertdialog = new alertdialog.builder(accountsetup.this); alertdialog.settitle("user account deletion"); alertdialog.setmessage("are sure delete user account?"); alertdialog.seticon(r.drawable.icon_alert); alertdialog.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int which) { tablerow9.setvisibility(view.gone); border9.setvisibility(view.gone);}}); alertdialog.setnegativebutton("no", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel();}}); alertdialog.show(); return true;}}); iconnext9.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent9 = new intent(accountsetup.this, accountsettingactivity9.class); startactivityforresult(intent9, 900);}}); //account 10 tablerow10 = (tablerow)findviewbyid(r.id.tablerow10); border10 = (view)findviewbyid(r.id.border10); iconnext10 = (button)findviewbyid(r.id.iconnext10); tablerow10.setonlongclicklistener(new onlongclicklistener() { public boolean onlongclick(view v) { alertdialog.builder alertdialog = new alertdialog.builder(accountsetup.this); alertdialog.settitle("user account deletion"); alertdialog.setmessage("are sure delete user account?"); alertdialog.seticon(r.drawable.icon_alert); alertdialog.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int which) { tablerow10.setvisibility(view.gone); border10.setvisibility(view.gone);}}); alertdialog.setnegativebutton("no", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel();}}); alertdialog.show(); return true;}}); iconnext10.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent10 = new intent(accountsetup.this, accountsettingactivity10.class); 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) { data.getstringextra("accountname1"); data.getstringextra("wanip1"); data.getstringextra("localip1"); data.getstringextra("port1"); data.getstringextra("password1"); if (resultcode == result_ok) { textview user1 = (textview)findviewbyid(r.id.user1); user1.settext(sp.getstring("accountname1", ""));}} else if ((requestcode == 150)&&(resultcode == result_ok)) { data.getstringextra("accountname1"); data.getstringextra("wanip1"); data.getstringextra("localip1"); data.getstringextra("port1"); data.getstringextra("password1");} 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 == 250)&&(resultcode == result_ok)) { data.getstringextra("accountname2"); data.getstringextra("wanip2"); data.getstringextra("localip2"); data.getstringextra("port2"); data.getstringextra("password2");} 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", "")); } }
below snippet of code use start d row-click in b since code long, try make clearer~
tablerow1.setonclicklistener(new onclicklistener() { public void onclick(view v) { intent intent1 = new intent(accountsetup.this, bluguardproto2activity.class); intent1.putextra("activity", "activity1"); startactivityforresult(intent1, 150);}});
i'm not sure understand problem i'm gonna give try anyway. want know 1 out of 2 different options in activity b used launch activity d? couldn't pass activity d when it's launched using intent.putextra()?
something like:
public class activityb extends activity { private void startactivitydfromfirstchoice() { intent = new intent(this, activityd.class); i.putextra("choice", "first"); startactivityforresult(i, 1234); } private void startactivitydfromsecondchoice() { intent = new intent(this, activityd.class); i.putextra("choice", "second"); startactivityforresult(i, 1234); } } public class activityd extends activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); string choice = getintent().getstringextra("choice"); if(choice != null && choice.equals("first")) { // activity launched first choice in b } else if(choice != null && choice.equals("second")) { // activity launched second choice in b } } }
now, example far perfect (passing strings ints or similar might better) illustrates mean.
it seems trying similar example note 2 things:
- i can't seem find place in activity d extras string pass activity b. have add getintent().getstringextra("activity") somewhere in activity d.
- in 1 place intent1.putextra("activity1", "activity1"); while in other intent2.putextra("activity", "activity2"); shouldn't "activity" in both cases extras key?
Comments
Post a Comment