开发者

Exception handling and lifecycle on Android?

I am trying to learn and test exceptions in android and can't seem to get information past try/catch, Thread.currentThread().setUncaughtExceptionHandler(new MyExceptionHandler()) and ACRA.

Basically I want开发者_如何学Python to know what happens when an Exception is thrown, does it kill only the current activity? What if my application has multiple activities and its killed in the 4th activity that is in the stack of activities, does it kill just the one?

I ask mainly because I am throwing an error and its not killing the complete program but restarting it at some middle activity of the program. (I am doing this on purpose to learn more about it) I would either prefer the program dies, than restart in some bad state, but Android has no kill switch (at least that I know of).

Thanks

EDIT: If android recreates the stack, do we know how far it goes? or if it uses create? or resume? What if the previous activities in the stack needed user input?


What I have observed from the LogCat is that android sort of caches the intents and tries to re-construct your stack from those. What I mean is, say you activity stack consists of Activity A, B, C, D (D on top, A at bottom) and an exception is thrown in Activity D. Then the OS tries to reconstruct the stack by firing intents for A, then B and then C. But I'm not sure about it


My impression has been that the Android framework prints out a stack trace from the uncaught exception then restarts the VM with everything but the Activity that caused the exception. For example, I had an uncaught Null Pointer exception in an activity and a background sticky service running. Android attempted to restart the VM and restart the sticky service, but I incorrectly implemented the service to always look at the intent starting it which is NULL on restart causing a second exception immediately afterwards.

Also, at least in Eclair and above, Android will attempt to restart a service after certain crashes such as an uncaught exception in onStartCommand(). In that case, onStartCommand() will be called with the flag START_FLAG_RETRY.

Some insight into this is here: http://groups.google.com/group/android-developers/msg/0eb714f48d534443


Android has no kill switch

You might try System.exit(int), from Java SE, which kills the virtual machine.

If android recreates the stack, do we know [...] if it uses create? or resume?

onCreate is called (see http://developer.android.com/images/activity_lifecycle.png)

BTW, an Exception does kill the program, but the ActivityManager restarts it. If the previous Activities required user input on which the front Activity depends, it should be put into the Bundle which is put into the Intent which starts the front Activity, because the system saves the Bundle and passes it again to the Activity when restarting it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜