onDestroy() 在 this.finish() 之后不会被调用

发布于 2024-09-05 12:44:54 字数 944 浏览 3 评论 0原文

我想知道为什么带有 2.1-update1 的 Motorola Milestone 的行为与模拟器或 Nexus One 不同。我正在尝试通过以下方式退出我的应用程序:

@Override
protected void onPause() {
    if(mayDestroyActivity) this.finish();
    super.onPause();
}

这在模拟器或 Nexus One 上运行良好。 onDestroy()onPause()onStop 之后立即调用。但不适用于里程碑。相反,当另一个 Activity 启动时,onDestroy() 会被调用。它在清单中的部分如下所示:

<activity android:name=".MyActivity"  
    android:configChanges="orientation|keyboardHidden"  
 android:label="@string/questionnaire_item"
 android:launchMode="singleInstance"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
 android:windowSoftInputMode="adjustPan">
 <intent-filter>
  <category android:name="android.intent.category.OPENABLE" />
 </intent-filter>
</activity>

有人对此有提示吗?我的应用程序依赖于正确退出,因为我在 onDestroy() 中保存了所有进度,

谢谢,
斯特夫

I'm wondering why the Motorola Milestone with 2.1-update1 behaves differently from the Emulator or e.g. the Nexus One. I am trying to exit my app with:

@Override
protected void onPause() {
    if(mayDestroyActivity) this.finish();
    super.onPause();
}

This works well on either Emulator or Nexus One. onDestroy() gets called immediatly after onPause() and onStop. But not for the Milestone. Instead, onDestroy() gets called when another Activity is started. Its section in the Manifest looks like this:

<activity android:name=".MyActivity"  
    android:configChanges="orientation|keyboardHidden"  
 android:label="@string/questionnaire_item"
 android:launchMode="singleInstance"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
 android:windowSoftInputMode="adjustPan">
 <intent-filter>
  <category android:name="android.intent.category.OPENABLE" />
 </intent-filter>
</activity>

Does anyone have a hint on this? My app depends on exiting properly since I save all progress in onDestroy()

Thanks,
Steff

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花间憩 2024-09-12 12:44:54

你做错了。这就是医生所说的:

注意:不要依赖此方法
被称为拯救的地方
数据! [...] 在某些情况下
系统将简单地杀死
活动的托管过程没有
调用此方法(或任何其他方法)
它,所以它不应该被用来做
打算保留的东西
进程消失后左右。

http://developer.android.com/reference/ android/app/Activity.html#onDestroy%28%29

You are doing it wrong. This is what the Doc says:

Note: do not count on this method
being called as a place for saving
data! [...] There are situations where
the system will simply kill the
activity's hosting process without
calling this method (or any others) in
it, so it should not be used to do
things that are intended to remain
around after the process goes away.

http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29

十年九夏 2024-09-12 12:44:54

如果用户退出到主屏幕,这并不意味着应用程序应该退出。在大多数情况下,如果应用程序只是转到后台,则会提供更好的用户体验。尝试依赖 saveInstanceState 和 onPause。

在 Android 世界中,退出应用程序被视为不好的做法,如果应用程序在按下主屏幕后继续运行,则具有良好的真正多点触控系统的手机可以获得更好的用户体验。

If the user exits to the home screen this does not need to mean that the app should be exited. In most of the time it will give a better user experience if the app just goes to the background. Try to rely on saveInstanceState and onPause.

Exiting a app is seen as bad practice in the android world, featuring a phone with a good system for true multitouch can get better user experience if the app keeps running after pressing the home screen.

眼趣 2024-09-12 12:44:54

您应该使用onSaveInstanceState,检查这个 链接。

You should use onSaveInstanceState, check this link.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文