简体   繁体   English

如何隐藏我的应用程序但不关闭/完成它

[英]How to hide my application but not close/finish it

I would like to let user to leave my application by hiding it but not finishing it. 我想让用户通过隐藏它但不完成它来离开我的应用程序。

I could call finish() in my main activity, but it takes some time to do that. 我可以在我的主要活动中调用finish(),但这需要一些时间。 It's not good for user experience, therefore a better choice might be to hide it. 这对用户体验不利,因此更好的选择可能是隐藏它。 Just I don't know how to achieve it. 只是我不知道如何实现它。

Thanks for all the answer. 谢谢你的回答。


I am not trying to do something in the background, because I already have a service. 我不是想在后台做点什么,因为我已经有了服务。

In my application, user might press an button to close my main activity. 在我的应用程序中,用户可能会按一个按钮来关闭我的主要活动。 However, it take a little time to do that. 但是,这需要一点时间。 At least after 0.5 sec, then I'll see my application disappear on screen. 至少0.5秒后,我会看到我的应用程序在屏幕上消失。

However, if I press home key. 但是,如果我按主页键。 My application is disappear immediately, so it's the effect I need. 我的应用程序立即消失,所以这就是我需要的效果。

您可以尝试在后台移动您的活动

moveTaskToBack(true);

If you need your app to continue running in the background, spawn the background code in a Service. 如果您需要应用程序继续在后台运行,请在服务中生成后台代码。

http://developer.android.com/reference/android/app/Service.html http://developer.android.com/reference/android/app/Service.html

The best advice here is simply to start looking at all of the garbage collection the app has to do and make sure you only keep things (connections, etc) open for as long as absolutely necessary. 这里最好的建议就是开始查看应用程序必须执行的所有垃圾收集,并确保只在绝对必要时保持打开(连接等)。

Point is to limit the amount of stuff the app has to do to finally shut down. 要点是限制应用程序必须做的事情才能最终关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM