I am building an Android App. How to exit an Android App when back is pressed. Android version is 2.3.3 and above. The Android App goes to previous activity which i don\'t want.
// Finish current activity
// Go back to previous activity or closes app if last activity
finish()
// Finish all activities in stack and app closes
finishAffinity()
// Takes you to home screen but app isnt closed
// Opening app takes you back to this current activity (if it hasnt been destroyed)
Intent(Intent.ACTION_MAIN).apply {
addCategory(Intent.CATEGORY_HOME)
flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(this)
}