Exit android app on back pressed

后端 未结 12 2111
逝去的感伤
逝去的感伤 2021-01-30 10:57

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.

12条回答
  •  野性不改
    2021-01-30 11:36

    // 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)
    }
    

提交回复
热议问题