java - How call method from second activitty and return value -
i have 2 activity, first on activity "loginactivity",and second activity "student_ activity". please till me how call method "call" second activity , return value bool first activity know user if id , password correct or not correct. first activity id , password "edittext" send id , password method in second activity sure data server.
code first activity :
public class loginactivity extends activity{ edittext edtid; edittext edtpassword; button btn1; sharedpreferences prefs; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.login); prefs = this.getsharedpreferences(this.getapplicationcontext().getpackagename(), context.mode_private); edtid=(edittext)findviewbyid(r.id.idstudent); edtpassword=(edittext)findviewbyid(r.id.passwordstudent); btn1=(button)findviewbyid(r.id.btnlogin); btn1.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { createloginsession(integer.parseint(edtid.gettext().tostring()),edtpassword.gettext().tostring()); //here call second activity sure data intent intent = new intent(loginactivity.this,tofi.android.student_activity.class); startactivity(intent); finish(); startactivity(new intent(loginactivity.this,com.jcxavier.widget.test.badgebuttontestactivity.class)); } }); } //this method store data in sharedpreferences data in second activity public void createloginsession(int id, string password){ sharedpreferences.editor editor = prefs.edit(); editor.putint("id", id); editor.putstring("password", password); editor.commit(); } }
code second activity is:
public class student_activity { sharedpreferences prefs = this.getsharedpreferences(this.getapplicationcontext().getpackagename(), context.mode_private); public void oncreate(bundle icicle) { super.oncreate(icicle); setcontentview(r.layout.program_student); newsloader call = new newsloader(); call.execute(this, true); } private context context; private boolean pullfromserver = false; class newsloader extends asynctask<object, void, list<student>> { private context context; private boolean pullfromserver = false; protected list<student> doinbackground(object... params) { context = (context) params[0]; pullfromserver = (boolean) params[1]; datasource.open(); if (pullfromserver) { //get attribute sharedpreferences int id = prefs.getint("id", 24); string password = prefs.getstring("password","wce"); // studenthandler class sure password content method call send server , //return value if correct or not correct , return value type bool. bool s; s = studenthandler.getinstance().call(context,id,password); } } }
1. call startactivityforresult()
(documentation) , override onactivityresult()
(documentation) in first activity.
2. in second activity perform whatever validation need (this done in first activity passing data via intent
) , call setresult(int resultcode, intent data)
(documentation) , finish();
second activity.
if using startactivityforresult()
not feasible situation, can use setresult()
, startactivity(), pass data need via intent, , validate in onactivityresult().
i skimmed on this, here's example of in action.
Comments
Post a Comment