How to use the finish() method in Android [closed]
Asked Answered
V

1

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?

Verdaverdant answered 4/1, 2011 at 15:4 Comment(2)
Your question is not very clear, do you want Activity A to start a new Activity B, and for Activity A to finish?Kirkpatrick
I agree with dave.c and further more, why would you want to?Noncompliance
V
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.

Verdaverdant answered 2/4, 2011 at 6:57 Comment(4)
What happens if u don't call the finish function? Does it really make a difference in foreground? TyAdit
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.Opinionated
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 #4038979 If you wish to continue to the next activity, please use this code: Intent i = new Intent(CurrentActivity.class, NextActivity.class); startActivity(i)Opinionated
Also see this documentation for the finish method of Activity: developer.android.com/reference/android/app/…Opinionated

© 2022 - 2024 — McMap. All rights reserved.