5

I am a new beginner in android, so please tell me how to finish an activity in Android. I have already done one project. In this project, the previous activity was also working. So please tell me how to finish that activity when the next activity starts briefly.

Thanks in advance

Ypsilon
  • 120
  • 1
  • 9
Ramakrishna
  • 4,018
  • 15
  • 47
  • 71

1 Answers1

11

After I start the activity, I call the finish() method:

Intent i=new Intent(mainclass.this,nextclass.class);
startActivity(i);
finish();

Then the previous activity(mainclass) is finished.

Ypsilon
  • 120
  • 1
  • 9
Ramakrishna
  • 4,018
  • 15
  • 47
  • 71
  • What happens if u don't call the finish function? Does it really make a difference in foreground? Ty – Rishav Jun 03 '17 at 18:29
  • The problem could be the question, but I don't recommend this code because you shouldn't need to call startActivity and finish at the same period. `startActivity` sends you to the next screen and `finish` will send you back to the previous screen. I tested this code and while it did result in "nextclass" being called, it resulted in an infinite loop when using the back button. – Dagmar Dec 14 '17 at 08:58
  • Unfortunately I'm unable to add my own answer to this question because it is closed. If you wish to go back to the previous activity, see this post https://stackoverflow.com/questions/4038479/android-go-back-to-previous-activity If you wish to continue to the next activity, please use this code: `Intent i = new Intent(CurrentActivity.class, NextActivity.class); startActivity(i)` – Dagmar Dec 14 '17 at 09:00
  • Also see this documentation for the finish method of Activity: https://developer.android.com/reference/android/app/Activity.html#finish – Dagmar Dec 14 '17 at 09:03