Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

android - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...