3

I have a class which extends Application. In its onCreate() I have:

    Thread.currentThread().setUncaughtExceptionHandler(
            new UncaughtExceptionHandler()
            {               
                 @Override
                 public void uncaughtException(Thread thread, Throwable ex)
                 {
                      System.exit(0);
                 }
             });

expecting my app to go off forever. But after System.exit(0) is called, android restarts my application as if I've launched it myself. Why?

By the way, do you know any way to show an AlertDialog notifying the user of the uncaught exception?

Joseph_Marzbani
  • 1,796
  • 4
  • 22
  • 36
  • 1
    why you're calling System.exit? I thing be a better idea uses the finish method. – ademar111190 Feb 13 '14 at 16:08
  • 2
    finish() is a method of Activity. Like I said, I'm handling uncaught exceptions in my Application instance. So finish() is simply impossible. – Joseph_Marzbani Feb 13 '14 at 16:09
  • Why aren't you just letting uncaught exceptions kill the app like normal? – Khantahr Feb 13 '14 at 16:10
  • 1
    Because I'm going to ask the user if they want a few log files to be sent to me. – Joseph_Marzbani Feb 13 '14 at 16:12
  • Most likely your app isn't restarting - the current activity is killed and you are seeing the next one on the activity stack. Look at integrating ACRA into your app - it handles crashes elegantly and allows crash reports to be sent to you in whatever manner you wish. – NigelK Feb 13 '14 at 16:14
  • You don't need the exit because that is already taken care of, you should just implement the Log storing and sending, implement ACRA for crash reports! EDIT: beat me to it ;) – Pontus Backlund Feb 13 '14 at 16:14
  • Setting exception handler on current thread only doesn't seem to be a good solution. – Grzegorz Żur Feb 13 '14 at 16:17
  • try it http://stackoverflow.com/questions/4732184/how-to-finish-an-android-application – ademar111190 Feb 13 '14 at 16:17
  • 1
    But when I delete that line (System.exit(0)) the app freezes in the last state it was. Nothing happens for ages. – Joseph_Marzbani Feb 13 '14 at 16:18
  • #Grzegorz: I cannot see your point. Then how can I handle UNCAUGHT exceptions? Isn't that what setUncaughtExceptionHandler has been designed for? – Joseph_Marzbani Feb 13 '14 at 16:19
  • #NigelK: No, onStart() was called indicating it's a new start. – Joseph_Marzbani Feb 13 '14 at 16:29

1 Answers1

-1

You can not use System.exit() in Android, it causes undesired behavior as you have found. If you must terminate an activity, use finish() instead and let Android clean it up.

Khantahr
  • 8,156
  • 4
  • 37
  • 60
  • finish() is a method of Activity. Like I said, I'm handling uncaught exceptions in my Application instance. So finish() is simply impossible. – Joseph_Marzbani Feb 13 '14 at 16:13
  • By the way, all I want to do is to show the user an AlertDialogue and ask them if I'm allowed to email a few log files or not. Any idea? – Joseph_Marzbani Feb 13 '14 at 16:14
  • When your app crashes, Android asks the user if they want to report it, which sends a bug report with logs to your developer console. What do you want to accomplish that isn't covered by that? – Khantahr Feb 13 '14 at 16:19
  • #Ralgha: The only thing I see is "unfortunately ... is closing unexpectedly". – Joseph_Marzbani Feb 13 '14 at 16:21
  • If you're installing over ADB you won't see it, you only get that option if it's installed via the Play Store. – Khantahr Feb 13 '14 at 17:50