Close an application on android

0

I'm doing an application on android, and when I've reached an activity, I want the user to press the backward key and not go back to the previous screen but go back to the phone's initial menu.

The application would be like this: when entering the application I enter activity A, from that I would go to activity B and later to C or D. But if from C or D I go backwards I want it to go directly to the beginning of the phone and that the application stays in the backStack. Menu Start-> A-> B-> C or D I do not want the screen from D to go back to A and there it comes out. Can that be done?

In short, the question is how you can exit an application from any activity and that it stays in the backStack.

Thanks in advance.

    
asked by javier 14.07.2017 в 18:04
source

1 answer

1

Use the onBackPressed method, within the method you can attach the actions you want your application to perform.

    @Override
    public void onBackPressed() {

        super.onBackPressed();
    }

You can also use the onKeyDown

method
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //return true;
           //agregas la logica/acciones a realizar
        }
        return super.onKeyDown(keyCode, event);
    }
    
answered by 14.07.2017 в 19:08