Android how to close a running app on back press?

Amna Mirza picture Amna Mirza · Oct 30, 2015 · Viewed 10.5k times · Source

How to exit from an app on back press of main activity? I am using this code, but its finish main activity and show other activities from back stack.

@Override
    public void onBackPressed() {
        finish();
    }

Answer

Amna Mirza picture Amna Mirza · Oct 30, 2015

I tried this and its work for me really well,as I am working on app, where once session get expired, user is able to see only few views and remain hidden.

    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
    }