简体   繁体   English

Android-恢复活动(被销毁时)

[英]Android - Resume activity (when destroyed)

I have say ActivityA , when i press the back button, I call moveTaskToBack(true) in order to put it in the background ( onPause() is called). 我说过ActivityA ,当我按下后退按钮时,我调用moveTaskToBack(true)以便将其置于后台(调用了onPause() )。 When I go and do other stuff in phone, the system will eventually destroy my ActivityA and onCreate() will be called when I return to my ActivityA . 当我去电话里做其他事情时,系统最终将破坏我的ActivityA ,当我返回到ActivityA时将调用onCreate() How can I resume the activity instead of creating it when this happens? 在发生这种情况时,如何恢复活动而不是创建活动?

Many thanks! 非常感谢!

I have to admit not being entirely known with moveTaskToBack();, but it doesn't sound to me like you can use this to affect the Activity life cycle. 我必须承认对moveTaskToBack()并不完全了解,但是听起来不对,因为您可以使用它来影响Activity的生命周期。

It sounds more like simply effecting the 'breadcrumbs' you leave, which affects the way your back-button works. 听起来更像是影响离开的“面包屑”,这会影响后退按钮的工作方式。 You're just placing your Activity to the back of this stack, so it won't get back with the back-button firstly I think. 您只是将Activity放置在堆栈的后面,所以我认为它不会首先使用后退按钮。

Secondly, your Activity will not go into onPause when leaving it, but onStop. 其次,您的活动离开时不会进入onPause,而是进入onStop。 onPause is for when per example a dialog opens over your Activity. onPause用于每个示例在“活动”上打开对话框时。

You can't stop the system from destroying your Activity when needed, and you can't stop it from calling onCreate after this happens. 您无法阻止系统在需要时销毁您的Activity,也不能阻止它在发生这种情况后调用onCreate。 You can use the Bundle savedinstancestate to save data in, so you don't have to re-fetch these from the database, internet, file dir or whatever. 您可以使用捆绑的saveinstancestate状态保存数据,因此您不必从数据库,Internet,文件目录或其他任何内容重新获取这些数据。

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

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