How to add session in android application? -
i'm making android application in there different activities connected home (mainactivty) activity , vice-verse. want close application whenever press key home activity. takes me previous activity came from. how can this. please me in this, i'm new android. thanks.............
here code:
btn_daily.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent = new intent(getapplicationcontext(), activitya.class); startactivity(i); } }); btn_health.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent = new intent(getapplicationcontext(), activityb.class); startactivity(i); } }); btn_diet.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent = new intent(getapplicationcontext(), activityc.class); startactivity(i); } }); btn_exercise.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent = new intent(getapplicationcontext(), activityd.class); startactivity(i); } }); btn_yoga.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent = new intent(getapplicationcontext(), activitye.class); startactivity(i); } }); btn_help.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent = new intent(getapplicationcontext(), activityf.class); startactivity(i); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } public boolean onkeydown(int keycode, keyevent event) { if (keycode == keyevent.keycode_back) { dialogonbackpress(); return true; } return super.onkeydown(keycode, event); } protected void dialogonbackpress() { new alertdialog.builder(this) .settitle("exit alert !!") .setmessage("are sure want exit?") .setcancelable(false) .setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { finish(); } }) .setnegativebutton("no", null) .show(); }
it seems activities other mainactivity
, has mechanism navigate other activities. while navigating other activity, don't forget use finish()
destroy current activity.
and if application has other mechanism there home button takes home screen navigation activity, @ time open mainactivity
flag_activity_clear_top
flag.
hence doing so, not have stack of activity , button mainactivity
close app.
edit : scenario have mentioned in comment, have use following code on home button click.
btn_home.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent = new intent(currentactivity.this, mainactivity.class); i.setflags(intent.flag_activity_clear_top); startactivity(i); finish(); } });
Comments
Post a Comment