2015-05-15 224 views
1

我想我的應用程序,如果用戶按下回在某個活動退出。以下是該活動的代碼。退出應用程序上後退按鈕按不工作

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(); 
     } 
    } 

} 

它需要應用程序兩次後退按鈕(和兩個確認)才能退出。什麼解釋了這種行爲,有什麼辦法可以解決這個問題?下面

+0

西隧hppn ... – koutuk

+0

嘗試加入'super.onBackPressed();''之前完成();' –

回答

0

移除onBackPress()方法片段。它將只處理SlideMenu

if (menu.isMenuShowing()) { 
    menu.showContent(); 
} 

代碼從SlidingFragmentActivity通話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)將調用SlidingActivityHelper一個方法:

public boolean onKeyUp(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK && mSlidingMenu.isMenuShowing()) { 
     showContent(); //Close the SlidingMenu and show the content view. 
     return true; 
    } 
    return false; 
} 
+0

@Sandah昂是你的問題解決了嗎? –

0

super.onBackPressed();會做你想要什麼都,只是包括super.onBackPressed();代替System.exit(0);,因爲它會調用onBackpressed O法˚FActivity。所以,onDestroy方法得到正確調用。

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(); 
            super.onBackPressed(); 
           } 
          }).setNegativeButton("No", null).show(); 
在第一次記者
相關問題