Search code examples
androidback

Confirm back prompt to exit app


I'm currently working on an app that uses a lot the back button of android devices. I was wondering how to check if the activity is the last one and if the user presses back again, it doesn't exit the app except if the user presses back again, like with the 9gag app.

Thank you!


Solution

  • With the method Activity.isTaskRoot() you can check if the user will exit the app on pressing the back button.

    Then you just have override onBackPressed() to handle it differently than usual.