97

What is the equivalent operation within an activity to navigating away from the screen. Like when you press the back button, the activity goes out of view. How can this be called from inside an activity so that it closes itself.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Androider
  • 21,125
  • 36
  • 99
  • 158

5 Answers5

182

What about the Activity.finish() method (quoting) :

Call this when your activity is done and should be closed.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
49

you can use this.finish() if you want to close current activity.

this.finish()
Soheil
  • 743
  • 6
  • 3
5

you can use finishAffinity(); to close all the activity..

DanielBarbarian
  • 5,093
  • 12
  • 35
  • 44
3

finish() method is used to finish the activity and remove it from back stack. You can call it in any method in activity. But make sure you close all the Database connections, all reference variables null to prevent any memory leaks.

Ginni
  • 136
  • 1
  • 2
2

You Can use just finish(); everywhere after Activity Start for clear that Activity from Stack.

  • 3
    Hi Raj, welcome to Stackoverflow. When answering questions, it's generally a good idea to check other answers to that question - someone might have already had the same idea and posted the answer! Which is exactly the case here. The top answer, and the accepted one already says `finish()` - so there isn't much to be gained for you or the community by repeating it. Sorry to be the bearer of bad news. In any case welcome to Stackoverflow, thanks for trying - hope you have better luck next time! – kenny_k Sep 17 '19 at 12:15