Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity.finish() called but activity stays loaded in memory

When I run my app on the debugger, I get the main thread and 3 binder threads.

On a button click I call Activity.finish(), which looks like it ends the activity as the UI closes and goes back to the home screen.

However, in the debugger, it still shows the main thread and 3 binder threads as "(running)".

I am baffled to why this is happening. Even more so, it is causing my app to call Activity.onResume() when I run it again after exiting the app.

I currently override these methods in the Activity, but I call the appropriate super functions in each one:

  • onDestroy()
  • onPause()
  • onResume()
  • onSaveInstanceState()

Any help or advice regarding this is much appreciated!

like image 830
twig Avatar asked Jan 04 '11 15:01

twig


People also ask

What does finish () do in Android?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

What function do you call to end an activity and return to the previous activity?

You can do the same thing as the Back button, that is, close the current activity by calling a function named finish.


1 Answers

You don't control when your app leaves main memory, the OS does. Look closely at Activity.finish...

Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().

Note that is says nothing about memory. As to calling Activity.onResume, that's exactly what you would expect for the lifecycle; remeber that onResume is not just called after a resume but even when the app is first launched after onCreate.

While not exactly what you asked I suggest you read this article about exit buttons which goes on to say something very important

[Activity.finish] is exactly equivalent to hitting the back button.

like image 146
Andrew White Avatar answered Jan 04 '23 08:01

Andrew White