Is it a good idea to call finish() after starting a new Activity in Android?
Asked Answered
A

3

28

Like:

startActivity(intent);
finish();

Without calling finish() explicitly, onDestroy() is not called for the former Activity, and I run out of memory (OutOfMemory Exception).

So, is it a good idea to call finish() explicitly to prevent OutOfMemory Exception?

Alysa answered 7/8, 2013 at 18:40 Comment(4)
what if user wants to return to previous activity by clicking back button?Cinquecento
No. Your problem is somewhere else.Maighdiln
Are you using Bitmap (or a collection of) anywhere? If you haven't yet used mat Memory Analyzer Tool, now is a good time.Declarer
When you call finish() , you are removing the activity from backstack basically , so if you move to another activity and decides to come back for example through on backpress , it will crash because the activity no longer exists in the backstack so that it gets called , the only way to go back to an activity even after removing it from the backstack by creating an intent so simple creating a new instance of that activityProtect
W
41

When you start a new activity, the current activity is pushed onto the back stack of the current task. (You can change this behavior via flags and/or the manifest, but this is the default behavior.) When the user presses the back function, the top activity is finished and the stack is popped. The result is that the user sees the app return to the previous activity.

It's perfectly fine to call finish() after starting a new activity. The result will be that the current activity (which is no longer at the top of the stack, since you just started a new one) will be removed from the stack. Then when the user presses Back, it will go to the previous activity on the back stack (or exit your app if the stack is empty).

If you are bouncing back and forth between, say, activities A and B by always starting a new one and never calling finish(), this can cause an OOM exception as the stack fills up with instances of each activity.

You can read more about this in the guide topic Tasks and Back Stack. It also describes how to deal correctly with cycling between activities.

Welby answered 7/8, 2013 at 18:45 Comment(6)
Does the order of startActivity(intent); finish(); these two statements matter at all?Cardie
@WeishiZeng - I've done it both ways and it seems to make no difference. I don't think it matters.Welby
@TedHopp Thanks! I've also seen both working. But just curious that, is it possible that finish() leads to destruction of current activity instance before startActivity(intent) happens? Then it's problematic as both intent and startActivity() won't exist without its associated instance.Cardie
@WeishiZeng - I don't think there's a problem. The activity might be destroyed (although that may happen on a subsequent pass of the UI loop) but the activity object instance still exists.Welby
@TedHopp By "destroyed", do you mean that 1) onDestroy() called, and 2) removed from task stack? But the activity instance is not eligible for gc?Cardie
@WeishiZeng - Yes, I meant both. I'm not sure if onDestroy() is called immediately, but it should make no difference. The activity instance is not eligible for gc because the system has a reference to the intent, which has a reference to the activity. So as far as the gc is concerned, the instance is still alive.Welby
B
2

Doing this is fine if you don't need an instance of that Activity. So when you press back on the next Activity know that you won't come back to this one but whatever is on the stack below where that Activity was or the home screen if there are no more.

However, I'm not sure this is why you are getting an OOM exception and you should probably figure out where that is coming from instead. If you are using Bitmaps then that could be causing the exception.

Brambling answered 7/8, 2013 at 18:43 Comment(1)
Yes, I use Bitmaps. Without calling explicitly finish(), GC doesn't call onDestroy() of the Activity, and the Activity's loaded Bitmap resources takes up much memory, therefore I end up outOfMemoryException. If onDestroy() is not called by GC does it mean a memory leak? If there is no reference to the Activity, shouldn't the GC call ondestroy() when more memory is needed?Alysa
I
1

Alternatively in the manifest you could add android:noHistory="true" to the activity and when you navigate away from the activity it will not add it to the back stack.

Incantatory answered 25/8, 2021 at 3:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.