Activity.finish() 被调用但活动保持加载在内存中 [英] Activity.finish() called but activity stays loaded in memory

查看:32
本文介绍了Activity.finish() 被调用但活动保持加载在内存中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在调试器上运行我的应用程序时,我得到了主线程和 3 个绑定线程.

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

单击按钮时,我调用 Activity.finish(),这看起来像是在 UI 关闭并返回主屏幕时结束 Activity.

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.

但是,在调试器中,它仍然将主线程和 3 个绑定器线程显示为(running)".

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

我不明白为什么会发生这种情况.更重要的是,当我退出应用程序后再次运行它时,它会导致我的应用程序调用 Activity.onResume().

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.

我目前在 Activity 中覆盖了这些方法,但我在每个方法中调用了相应的超级函数:

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!

推荐答案

您无法控制应用何时离开主内存,而由操作系统控制.仔细查看 Activity.finish...

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

活动完成后调用并且应该关闭.这ActivityResult 被传播回谁让你通过onActivityResult().

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

请注意,这与内存无关.至于调用 Activity.onResume,这正是您对生命周期所期望的;请记住,onResume 不仅会在恢复后调用,甚至在 onCreate 之后首次启动应用时也会调用.

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] 完全等同于点击后退按钮.

这篇关于Activity.finish() 被调用但活动保持加载在内存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆