简体   繁体   中英

Exit App on Back Button Press not working

I would like my application to quit if the user presses back at a certain activity. The following is the code for that 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 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();
        }
    }

}

It takes the app two back presses (and two confirmations) to exit. What explains this behaviour and is there any way to resolve this?

Remove below snippet from onBackPress() Method. It will handle by SlideMenu only.

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

Code from 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) will call a Method of 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;
}

super.onBackPressed(); will do what ever you want,just include super.onBackPressed(); instead of System.exit(0); ,because it will call the onBackpressed method of Activity .So, onDestroy method get call properly.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM