应用程序部分关闭

发布于 2024-12-09 06:48:47 字数 633 浏览 1 评论 0原文

我正在使用服务和片段兼容性包构建应用程序,但遇到了一些奇怪的行为。我有一个 FragmentActivity 和一个实现 LoaderCallbacks 的 ListFragment,当 FragmentActivity 启动时,它会启动一项服务来下载数据并填充数据库。

假设我在该屏幕上,并且通过单击 ListFragment 的其中一行来导航过去。如果我然后点击空指针,或任何其他导致弹出常规强制关闭对话框的异常,我点击对话框上的强制关闭按钮,我所在的活动确实会被关闭,但我最终会回到FragmentActivity 屏幕,而不是我的 Android/启动器主屏幕。

对我来说,这表明应用程序正在崩溃,但不知何故片段活动正在重新启动。如果我在 Eclipse 的调试视图中点击红色停止按钮,就会发生这种行为 - 当前活动被终止,但该死的片段活动仍然存在。

显然,我不希望我的应用程序出现强制关闭对话框,但我真正想要弄清楚的是,这是否是我没有正确编码的症状。目前,我从未停止过该服务,主要是因为它在整个应用程序中一遍又一遍地重复使用,而且我还没有抽出时间编写一种方法来安全关闭服务,而不会过早结束对其的新请求。

该服务是否通过 ListFragment 和 LoaderCallback 或其他方式保留对 Activity 的引用?为什么这个FragmentActivity看似无敌呢?

I'm building an application using a service and the fragment compatibility pack and am running into some weird behavior. I have a FragmentActivity and a ListFragment which implements LoaderCallbacks, and when the FragmentActivity starts it kicks off a service to download data and fill in a database.

Suppose I'm on that screen, and I navigate past it by clicking on one of the rows of the ListFragment. If I then hit a nullpointer, or any other exception that causes the regular force close dialog to pop up, I hit the force close button on the dialog and the activity I'm on does indeed get shut down, but I end up back on the FragmentActivity screen, rather than say my Android/launcher home screen.

To me this says that the app is crashing but somehow the fragment activity is getting restarted. The behavior occurs if I hit the red stop button in the debug view in eclipse - the current activity gets killed but that damn fragment activity is still there.

Obviously I don't want my app to ever get a force close dialog, but really what I'm trying to figure out is if this is a symptom of me not coding something correctly. Currently I never stop the service, mostly because it gets reused over and over again throughout the app and I haven't gotten around to coding up a way to close the service safely without prematurely ending a new request to it.

Is the service keeping a reference to the Activity via the ListFragment and LoaderCallback or something? Why is this FragmentActivity seemingly invincible?

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

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

发布评论

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

评论(2

九局 2024-12-16 06:48:47

发生这种情况是因为您的进程已被终止,但活动堆栈上还有其他活动。这是预期的行为,因为一旦删除了违规活动,系统就会尝试在堆栈上的下一个活动上启动新进程。请参阅此处这里

如果您想在强制关闭后从堆栈中删除所有活动,您可以做的一件事就是点击后退按钮,直到进入主屏幕。除非您覆盖应用程序中后退按钮的行为,否则它会破坏您当前的活动并将您带到上一个活动。一旦没有更多的活动,您将被带到主屏幕,并且可以从 Eclipse 再次启动您的应用程序,它将从您的主要活动开始。

This is happening because your process is killed, but there are other activities on the activity stack. This is the expected behavior, as the system attempts to start a new process on the next activity on the stack once your offending activity was removed. See here and here.

If you want to remove all of your activities off the stack after a force close, one thing you can do is hit the back button until you are taken to the home screen. Unless you override the behavior of the back button in your app, it destroys your current activity and takes you to the previous activity. Once there are no more activities left, you will be taken to the home screen and can start your app again from eclipse, and it will start you off at your main activity.

最笨的告白 2024-12-16 06:48:47

我遇到了类似的问题,包括数据库加载和空指针异常。我不知道您的特殊情况,但如果您还没有这样做,那么将数据库分离到内容提供程序中可能会有所帮助,这样您就可以将片段与数据库更多地解耦,这可能有助于解决错误。

i had run into similar problems, with Databse loading and Nullpointer Exceptions. I don't know your special case but it might help to separate the Databse out into a Content Provider if you aren't already doing that, that way you are decoupling the Fragments form the Database more, it might help with the errors.

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