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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Sudish Kumar
Sudish Kumar

Written by Sudish Kumar

Flutter | Android | Node | Dart | Kotlin | Java | JavaScript | TypeScript | PHP | Short Story Teller | Tech Lead

No responses yet

Write a response