How Disable Back Button in Android Kotlin?

Sudish Kumar
1 min readNov 8, 2020

Let’s understand,

My purpose of using it : I have created a demo App in this I am using referral link for opening my aap . When I was entered in the aap from referral link it was showing login screen after login when I was pressing back button then it was showing two times login screen . But i want to close the app when user logged in and pressed back button aap should close.

For doing it we will use finishAffinity().

finishAffinity(): finish the current activity and all activities immediately below it in the current task that have the same affinity.

finishAndRemoveTask(): call this when your activity is done and should be closed and the task should be completely removed as a part of finishing the root activity of the task.

Let’s coding,

val intent = Intent(this,MainActivity::class.java)

intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP

startActivity(intent)

finish()

It’s working good .

That’s all, Thank you.

--

--

Sudish Kumar

Teach lead, Flutter Developer, Android Developer, Node Js Developer