我想我的應用程序,如果用戶按下回在某個活動退出。以下是該活動的代碼。退出應用程序上後退按鈕按不工作
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 you sure you want to exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
System.exit(0);
}
}).setNegativeButton("No", null).show();
}
}
}
它需要應用程序兩次後退按鈕(和兩個確認)才能退出。什麼解釋了這種行爲,有什麼辦法可以解決這個問題?下面
西隧hppn ... – koutuk
嘗試加入'super.onBackPressed();''之前完成();' –