5

As a newcomer to Android development, I would like to learn how to terminate an activity in Android. I have completed one project already and the previous activity functioned properly. Could you please explain how to finish that activity quickly when the next activity begins?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Ramakrishna
  • 4,066
  • 16
  • 48
  • 72

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
  • 122
  • 1
  • 9
Ramakrishna
  • 4,066
  • 16
  • 48
  • 72
  • 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