android - Exit App on Back Button Press not working -
i application quit if user presses @ activity. following code activity.
public class mainactivity extends fragmentactivity implements denkolistfragment.listselectlistener { private slidingmenu menu; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); displayview(0); menu = new slidingmenu(this); menu.settouchmodeabove(slidingmenu.touchmode_fullscreen); menu.setshadowwidthres(r.dimen.shadow_width); menu.setshadowdrawable(r.drawable.shadow); menu.setbehindoffsetres(r.dimen.slidingmenu_offset); menu.setfadedegree(0.35f); menu.attachtoactivity(this, slidingmenu.sliding_content); menu.setmenu(r.layout.menu_frame); menu.setselected(true); getsupportfragmentmanager() .begintransaction() .replace(r.id.menu_frame, new denkolistfragment()) .commit(); } private void displayview(int position) { fragment fragment = null; switch (position) { case 0: fragment = new mainviewfragment(); break; case 1: fragment = new stationviewfragment(); break; case 2: fragment = new mapviewfragment(); break; default: break; } if (fragment != null) { fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.content_frame, fragment).commit(); } else { } } @override public void select(int index) { displayview(index); menu.showcontent(); } @override public void onbackpressed() { if (menu.ismenushowing()) { menu.showcontent(); } else { new alertdialog.builder(this).seticon(android.r.drawable.ic_dialog_alert).settitle("exit") .setmessage("are sure want exit?") .setpositivebutton("yes", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { finish(); system.exit(0); } }).setnegativebutton("no", null).show(); } } }
it takes app 2 presses (and 2 confirmations) exit. explains behaviour , there way resolve this?
remove below snippet onbackpress()
method. handle slidemenu
only.
if (menu.ismenushowing()) { menu.showcontent(); }
code slidingfragmentactivity
call onbackpress
:
@override public boolean onkeyup(int keycode, keyevent event) { boolean b = mhelper.onkeyup(keycode, event); if (b) return b; return super.onkeyup(keycode, event); }
mhelper.onkeyup(keycode, event)
call method of slidingactivityhelper
:
public boolean onkeyup(int keycode, keyevent event) { if (keycode == keyevent.keycode_back && mslidingmenu.ismenushowing()) { showcontent(); //close slidingmenu , show content view. return true; } return false; }
Comments
Post a Comment