简体   繁体   English

返回键是否破坏了活动?

[英]Did back key destroy an activity?

I have an activity defined as below: 我的活动定义如下:

    <activity android:name="com.example.ui.HomeActivity"
        android:alwaysRetainTaskState="true"/>

A strange thing is that, when running on emulator, and the back key is pressed, the activity was destroyed (I saw onDestroy() called in log). 奇怪的是,当在模拟器上运行并按下返回键时,活动被破坏(我在日志中看到onDestroy())。 But when running on my Nexus One phone, and the back key is pressed, the activity is not destroyed (I didn't see onDestroy() called in log). 但是,当我在Nexus One手机上运行并按下返回键时,活动并未被破坏(我在日志中没有看到onDestroy())。

Could someone tell me why? 有人可以告诉我为什么吗?

Thanks. 谢谢。

When activity is in the background (after pressing Back key) it is always stopped but System can also decide to destroy it (eg. when system resources are low). 当活动处于后台(按“返回”键后)时,它始终会停止,但是系统也可以决定销毁它(例如,当系统资源不足时)。 You can't determine when it will be destroyed. 您无法确定何时将其销毁。

Activity can also be destroyed by calling finish() . 活动也可以通过调用finish()销毁。 You can determine that by checking isFinishing() status in onPause or onStop callback. 您可以通过检查onPauseonStop回调中的isFinishing()状态来确定。

Do not count on onDestroy callback. 不要指望onDestroy回调。 If system will kill activity it wont be called. 如果系统杀死活动,它将不会被调用。

By default, pressing the BACK key finishes (destroys) the current activity and displays the previous activity to the user. 默认情况下,按BACK键可完成(销毁)当前活动并向用户显示以前的活动。

source 资源

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

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