android - Back button when using the same activity for different views -
i have listview
populating data sqlite database , using same activity load different data in it, when row of listview
clicked displays information in activity
, question how handle button takes appropriate list.
1st button clicked :
intent i=new intent(hotel.class.this,activity_hotel); i.putextra("btn",1); startactivity(i);
2nd button clicked :
intent i=new intent(hotel.class.this,activity_hotel.class); i.putextra("btn",2); startactivity(i);
for button :
public boolean onkeydown(int keycode, keyevent event) { if (keycode == keyevent.keycode_back) { intent home_intent = new intent(getapplicationcontext(),hotel.class).setflags(intent.flag_activity_clear_top); startactivity(home_intent); return true; } return super.onkeydown(keycode, event); }
edited :
in mainactivity
case 2: intent intent3 = new intent(mainactivity.this,hotel.class); string lien="hotel"; intent3.putextra("choix",lien); startactivity(intent3); break; case 3: intent intent4 = new intent(mainactivity.this,hotel.class); lien="restaurant"; intent4.putextra("choix",lien); startactivity(intent4); break;
hotel.class :
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_hotel); mdbhelper = new databasehelper(this); try { mdbhelper.copydatabasefromasset(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } mdbhelper.opendatabase(); intent = getintent(); string choix = i.getstringextra("choix"); c = mdbhelper.fetchallservices(choix); //récupération des données startmanagingcursor(c); string[] fromcolumns = {databasehelper.key_name}; //from int[] toviews = {r.id.name}; // // définition des éléments de la liste dataadapter = new simplecursoradapter(this,r.layout.rows_services, c, fromcolumns, toviews, 0); list = (listview) findviewbyid(r.id.list); list.setadapter(dataadapter); //afficher la liste list.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> listview, view view, int position, long id) { nom = (textview) view.findviewbyid(r.id.name); // nom c'est memid_tv string nom_val = nom.gettext().tostring(); // nom_val c'est memberid_val intent modify_intent = new intent(getapplicationcontext(), extendlist.class); // extendlist c'est modify_member modify_intent.putextra("thename", nom_val); // thename c'est memberid startactivity(modify_intent); } });
details activity (extendlist.class )
protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.activity_list_daughter); dbhelper = new databasehelper(this); try { dbhelper.copydatabasefromasset(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } dbhelper.opendatabase(); name = (textview) findviewbyid(r.id.name1); adresse = (textview) findviewbyid(r.id.adresse); numtel = (textview) findviewbyid(r.id.tel); email = (textview) findviewbyid(r.id.email); website = (textview) findviewbyid(r.id.website); img = (imageview)findviewbyid(r.id.img1); intent = getintent(); string thename = i.getstringextra("thename"); // thename c'est membername ( memberid ) services = new service(); services = dbhelper.get_servicesname(thename); path = services.image; resources res = getresources(); int resid = res.getidentifier(path, "drawable", "com.example.guide_oran"); img.setimageresource(resid); name.settext( services.name); adresse.settext(services.adresse); numtel.settext(services.numtel); email.settext(services.email); website.settext(services.website); } public boolean onkeydown(int keycode, keyevent event) { if (keycode == keyevent.keycode_back) { intent home_intent = new intent(getapplicationcontext(), hotel.class).setflags(intent.flag_activity_clear_top); home_intent.putextra("choix",choix); // solution searching startactivity(home_intent); return true; } return super.onkeydown(keycode, event); }
hope i've provided details. ( problem solved, i've added solution post )
your problem might following snippnet:
public boolean onkeydown(int keycode, keyevent event) { if (keycode == keyevent.keycode_back) { intent home_intent = new intent(getapplicationcontext(), hotel.class).setflags(intent.flag_activity_clear_top); startactivity(home_intent); return true; } return super.onkeydown(keycode, event); }
actually, (even edited solution) new intent
causes problem return previous list. when return hotel.class
, reinitialize activity. then, listview removed , reuploaded.
to avoid this, why don't can call finish()
method destroy actual activity , return hotel
activity? being said, you not recreate activity , use resources again repopulate list.
i think, should use method handle state follows:
// use method handle button pressed public void onbackpressed() { super.onbackpressed(); // then, finish extendlist.class this.finish(); }
then extendlist
activity destroyed whereas hotel
activity, still behind, comes , displays previous list, without doing more.
let me know if helps.
Comments
Post a Comment