java - How to set Context menu to onBackPressedButton? -
i'm trying set context menu onbackpressedbutton "are sure quit?" here override onbackpressed
: don't know how call menu. should register here?
@override public void onbackpressed() { registerforcontextmenu(); return; }
or view should attach?
@override public void onbackpressed() { this.opencontextmenu(); return; }
should create viewlist
it? i've overridden oncreatecontextmenu
:
public void oncreatecontextmenu(contextmenu menu, view v, contextmenu.contextmenuinfo menuinfo) { menu.add(0,yes,0,"yes"); menu.add(0,no,0,"no"); }
i've overridden , oncontextitemselected
:
@override public boolean oncontextitemselected(menuitem item) { switch (item.getitemid()) { case yes: finish(); break; case no: break; } return super.oncontextitemselected(item); }
how must it?
just call onbackpressed()
method in class. find sample code below.
public void onbackpressed() { new alertdialog.builder(this) .settitle("really exit?") .setmessage("are sure want exit?") .setnegativebutton(android.r.string.no, null) .setpositivebutton(android.r.string.yes, new dialoginterface.onclicklistener() { public void onclick(dialoginterface arg0, int arg1) { intent intent = new intent(intent.action_main); intent.addcategory(intent.category_home); intent.setflags(intent.flag_activity_new_task); startactivity(intent); loginactivity.super.onbackpressed(); finish(); } }).create().show(); }
Comments
Post a Comment