1

I did caught all unHandled Exceptions in my android app like the common way:

public class AppContext extends Application{

    public static Context context;

    public void onCreate(){
        super.onCreate();

        AppContext.context = getApplicationContext();
        // Setup handler for uncaught exceptions.
        Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
        {
          @Override
          public void uncaughtException (Thread thread, Throwable e)
          {
            handleUncaughtException (thread, e);
          }
        });

    }

    public void handleUncaughtException (Thread thread, Throwable e)
    {
      Intent intent = new Intent (context, Login.class);
      intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); // required when starting from Application
      startActivity (intent);

      System.exit(1);
    }
} 

But despite that, the app sometimes(a little times) exits without any notice, and the log doesn't provide any crash messages, I don't know what else than an Exception can lead to that.

The app designed to be always running and not exiting whatever happens and that what makes me handle all uncaught Exceptions like the past way.

worth to mention some of the System.Err messages I got in the LogCat (don't know if it would help and don't know if I got it in the moment of app exiting problem or not and don't even know if I get it from the app or not as the logcat didn't mention that):

1:

Window type can not be changed after the window is added; ignoring change of com.android.internal.policy.impl.PhoneWindow$DecorView{426c3e00 V.E..... R.....ID 0,0-0,0}

2:

java.lang.SecurityException: WifiService: Neither user 10086 nor current process has android.permission.CHANGE_WIFI_STATE.
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • Why don't you start by adding `android.permission.CHANGE_WIFI_STATE` to your Manifest? – Machado Feb 19 '15 at 10:35
  • What's the use case for an architecture like that? As soon as the app crashes, you want to show a login Activity? Or do you want to send information about the crash somewhere? – Micky Feb 19 '15 at 10:39
  • it already added, I just posted all the system.err messages I got, I think it may be printed by another app – Muhammed Refaat Feb 19 '15 at 10:40
  • "The app designed to be always running and not exiting whatever happens and that what makes me handle all uncaught Exceptions like the past way." why are you force quitting the app then with `System.exit(1);` ? – Micky Feb 19 '15 at 10:40
  • 1
    @Kim , It seems like he is trying to auto-restart the app. – Jonas Czech Feb 19 '15 at 10:40
  • @Kim It's a company app not a commercial app, we don't want to catch logs we want to make the app always running – Muhammed Refaat Feb 19 '15 at 10:41
  • @JonasCz that's right – Muhammed Refaat Feb 19 '15 at 10:41
  • Then make a stable app that doesn't crash!? – Micky Feb 19 '15 at 10:41
  • @ Muhammed, You could end up with an endless restart, crash, restart... loop if your app crashes on start. – Jonas Czech Feb 19 '15 at 10:43
  • @Kim everyone hopes, I think this is not the problem though – Muhammed Refaat Feb 19 '15 at 10:43
  • @JonasCz it don't even gives the feeling of crashing, it just exits, and the app is so stable in it's start and it's static mode. – Muhammed Refaat Feb 19 '15 at 10:44
  • 1
    It just exits because you are calling `system.exit()` – Jonas Czech Feb 19 '15 at 10:47
  • 1
    See [this](http://stackoverflow.com/a/2903866/4428462) answer for how to auto-restart app on crash. – Jonas Czech Feb 19 '15 at 10:49
  • I agree with Kim. If you succeed in making your app auto-restart itself on crashes, then what you have made is an app that is more difficult to maintain. The best way to make an app reliable is 1) Let it crash 2) field the bug reports from users 3) fix the bugs, 4) create new release. Or better still test the app thoroughly *before* you put it in front of users. – Stephen C Feb 19 '15 at 10:51
  • @StephenC it is a dynamic app, it can't be tested 100%, sometimes an action by a new module, a memory leak, a server data problem can make it crashes, it a 30k loc app, so that I can came from any place, and it's not an app that can be downloaded and used from the app store, it's a company app that can be crashed due to any reason but must Never exists – Muhammed Refaat Feb 19 '15 at 10:57
  • 1
    @StephenC , Seems like he is trying to make some sort of a 'kiosk' which should not crash / should auto-restart on crash. – Jonas Czech Feb 19 '15 at 10:58
  • But as you see, a SecurityException like that happens before the Application reaches onCreate, so you can't handle that. Do you have the android.permission.CHANGE_WIFI_STATE set in your Manifest, like Machado suggested? – Micky Feb 19 '15 at 11:02
  • @JonasCz in fact I use `System.exit(1)` as a kind of killing any unwanted processes that can reserve parts in the memory or affecting the future process somehow, and in fact it works efficiently and the app restarted without problems – Muhammed Refaat Feb 19 '15 at 11:03
  • @Kim yea I have, may be this message is from another app in the device, the log didn't mention where it came from, I just added it because sometimes such thing can be the real problem while we don't see that coming. – Muhammed Refaat Feb 19 '15 at 11:04
  • @Muhammed , Yes, that is (kind of) fine, but that is why your app is just exiting (instead of crashing) , because that is what `system.exit()` does. See the link from my earlier comment for how to restart app on crash. – Jonas Czech Feb 19 '15 at 11:06
  • I don't know but if it crashes it will print a stack trace to the log, and that doesn't happen – Muhammed Refaat Feb 19 '15 at 12:35
  • @JonasCz - Well yea ... but he is going about it the wrong way IMO. He will end up with that something that doesn't "crash" but doesn't work properly either. – Stephen C Feb 19 '15 at 13:03

0 Answers0