Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android application crashes, but doesn't restart - resumes from another point in the application

The application in question has already been deployed. I have found out about ACRA, and will be adding that in to find out why the application crashes are happening.

My problem however, is that the application will crash (the dialog will show up giving you the option to either "Force Close" or "Wait"), but instead of restarting the application completely, it resumes from a point before the crash occurred. This causes problems however, that are only fixed by restarting the application (eg. sudden loss of data, crashes that don't make sense, etc.).

So how can I force my application, once crashed, to just end? (NOT restart).

Edit: The biggest issue is that after the application crashes, all the data in my preferences file disappears, until the app is restarted when it comes back.

like image 657
digerati32 Avatar asked Nov 16 '11 16:11

digerati32


2 Answers

Android will re-start the last active activity by default. Instead of forcing it to restart, the better strategy would be to fix your application, so that activities don't crash or exit gracefully even if no data is available. If you absolutely depend on some data (intent extras) and there are no defaults, you can just call finish() if it is not available, or start the main/parent activity.

Another way is to have your own default exception handler which re-starts the main activity, etc. after reporting the crash using ACRA (I believe there is a feature request for this in ACRA's bugtracker).

like image 147
Nikolay Elenkov Avatar answered Sep 28 '22 10:09

Nikolay Elenkov


I know this question is a bit old, but for future references and googlers, here is a complete answer from another, hard to find, stackoverflow question: Android App Restarts upon Crash/force close


create a class used to handle unCaughtException

public class MyExceptionHandler implements
        java.lang.Thread.UncaughtExceptionHandler {
    private final Context myContext;
    private final Class<?> myActivityClass;

    public MyExceptionHandler(Context context, Class<?> c) {

        myContext = context;
        myActivityClass = c;
    }

    public void uncaughtException(Thread thread, Throwable exception) {

        StringWriter stackTrace = new StringWriter();
        exception.printStackTrace(new PrintWriter(stackTrace));
        System.err.println(stackTrace);// You can use LogCat too
        Intent intent = new Intent(myContext, myActivityClass);
        String s = stackTrace.toString();
        //you can use this String to know what caused the exception and in which Activity
        intent.putExtra("uncaughtException",
                "Exception is: " + stackTrace.toString());
        intent.putExtra("stacktrace", s);
        myContext.startActivity(intent);
        //for restarting the Activity
        Process.killProcess(Process.myPid());
        System.exit(0);
    }
}

Then in every thread (usually you have just one, unless you start a new thread (Async or ... , ofcourse you know about threads if you are able to make new ;) ), set this class as the DefaultUncaughtExceptionHandler

Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler(this,
            YourCurrentActivity.class));

Remember though! Do it in the very last step of developing your application, you HAVE to at least try handle all of your exceptions one by one before leaving them for DefaultUncaughtExceptionHandler

like image 41
Muhammad Naderi Avatar answered Sep 28 '22 09:09

Muhammad Naderi