Android应用程序调用System.exit后工作不正常(0) [英] Android app behaving unexpectedly after calling System.exit(0)

查看:295
本文介绍了Android应用程序调用System.exit后工作不正常(0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个简单的应用程序有4个活动。我会形容它很快让你明白我想要的目的。

I'm writing a simple app with 4 activities. I'll describe it quickly so you understand what I'm trying to achieve.

第一项活动 - MainActivity有一定的文字编辑字段收集2输入参数 - 代表的数量和暂停的长度,单位为秒。 A键带我到第二个活动: WorkActivity - 这一切确实是它开始计数,直到我preSS'做',那么它要么调用PauseActivity,或者如果它已经过去的代表,呼吁OverviewActivity。 PauseActivity倒计时秒数直至下一个代表,然后发出蜂鸣声在我,让我知道它的时间,并再次表明WorkActivity。 OverviewActivity显示总的锻炼时间和次数的每个代表。

First activity - MainActivity has some TextEdit fields that collect 2 input parameters - number of reps and length of pause in seconds. A button takes me to the second activity: WorkActivity - all this does is it starts counting until I press 'done' then it either calls PauseActivity, or if it has been the last rep, calls OverviewActivity. PauseActivity counts down the seconds until next rep, then beeps at me to let me know it's time, and shows WorkActivity again. OverviewActivity shows total workout time and times for each rep.

此外,它还采用了应该刚刚结束的应用程序的按钮。我知道,退出你的应用是不是真的与Android的应用程序生命周期理念,但我需要它(或类似的东西发生)。 我有一个跟踪的代表和记录时的单控制器类。我可以杀死这种情况下(或假的死亡,所以一个新的将被创建),但是当我亲密的应用程序,然后再次单击它,我得到了OverviewActivity而不是预期的MainActivity。

It also features a button that should just end the app. I know exiting your apps is not really in line with the Android application life cycle philosophy, but I need it (or something like it to happen). I have a singleton controller class that keeps track of the reps and logs the time. I can kill this instance (or fake its death, so a new one will be created), but when I "close" the app and then click it again, I get the OverviewActivity instead of the expected MainActivity.

我预计,调用System.exit(0)会照顾的事情,简单地关闭应用程序,因此它必须再次运行时重新初始化。相反,整个事情开始演戏真的derpy。当我点击调用,而不是消失我的应用程序重新排序的System.exit(0),按钮。它显示了WorkActivity,并开始计数。当我点击完成按钮(这应该带我去PauseActivity)我得到一个异常。应用程序崩溃 - 然后再重新启动。这将重复,直到我打的主屏幕按钮,该应用程序保持在这个无用的状态,直到我杀了它在应用程序管理器。

I expected that calling System.exit(0) would take care of things, simply shut down the application, so it will have to initialize anew when run again. Instead the whole thing started acting really derpy. When I click the button that calls System.exit(0), instead of vanishing my app sort of restarts. It shows the WorkActivity, and starts counting. When I click the done button (which should take me to PauseActivity) I get an exception. The application crashes - and then restarts again. This will repeat until I hit the homescreen button, and the app remains in this useless state until I kill it in application manager.

另外,我不能完全肯定,但我认为System.exit(0)调用(或后续的崩溃)断开调试器,因为我一直无法得到Eclipse来后打任何断点。这意味着我真的不能看到所发生的实际的异常。

Also, I'm not exactly sure, but I think the System.exit(0) call (or the subsequent crash) disconnects the debugger, cause I've been unable to get Eclipse to hit any breakpoints afterwards. This means I can't really see the actual exception that occurs.

有人可以提供一些线索这光?有没有一种方法使用System.exit(0)是否正确?

Can someone shed some light on this? Is there a way to use System.exit(0) correctly?

在没有这个选项,这将是处理这个正确的方法是什么? 我需要的应用程序来: - 当我点击最后的完成按钮,主页按钮或后退按钮,处置控制器,(以及其他一切可能的话),停止计数(如果有的话定时器运行),基本上是自行关闭) - 当我再次单击该应用程序的图标,给我一个新的实例(或者出现新)与MainActivity来迎接我,并在其默认状态下的任何其他活动。

In the absence of this option, what would be the correct way of handling this? I need the app to: - when I click the final 'Done' button the Home button or the Back button, dispose of the Controller, (and everything else if possible), stop counting (if any timer is running) and essentially shut itself down) - when I click the app's icon again, to show me a new instance (or one that appears new) with the MainActivity to greet me and all other activities in their default state.

推荐答案

使用System.exit(0)是一个不好的做法。

Using System.exit(0) is a bad practice.

在这种情况下,调用exit()将终止该进程,杀死你   其他成分并有可能破坏你的数据。操作系统可以   不在乎,当然,但你的用户可能无法AP preciate吧。

Calling exit() in this case would terminate the process, killing your other component and potentially corrupting your data. The OS could care less of course, but your users might not appreciate it.

自愿杀死你的进程将不利于其他应用程序,如果   他们用尽了其内部的Dalvik的堆限制。不管怎样   多物理存储器设备具有中,OS具有多少的限制   内存的Dalvik是允许在任何过程中使用的堆分配。   因此,有可能的是,系统有它的内存和一个自由的半   特定的应用程序仍然命中OOM。

Killing voluntarily your process will not help other applications, if they have exhausted their internal Dalvik heap limit. No matter how much physical memory a device has, the OS has a limit on how much memory Dalvik is allowed to use in any process for heap allocations. Thus, it is possible that the system has free half of its memory and a particular application still hits OOM.

不要使用System.exit(0);相反,您可以只使用结束()。

Do not use System.exit(0); instead you can just use finish().

Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
finish();

这篇关于Android应用程序调用System.exit后工作不正常(0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆