Android application crashes, but does not restart - resumes work from another point in the application

This application has already been deployed. I learned about ACRA and will add this to find out why application crashes occur.

However, my problem is that the application crashes (a dialog box appears that gives you the option to either "Force close" or "Wait"), but instead of restarting the application, it resumes from the point before the crash occurred. However, this causes problems that are resolved only when the application is restarted (for example, sudden data loss, crashes that make no sense, etc.).

So, how can I make my application, once crashed, just finish? (DO NOT restart).

Edit: The biggest problem is that after the application crashes, all the data in my settings file disappears until the application restarts when it returns.

+6
source share
2 answers

Android will restart the last active action by default. Instead of forcing it to reboot, the best strategy would be to fix your application so that the actions do not fail or stop, even if there is no data. If you are absolutely dependent on some data (intentions) and there are no default values, you can simply call finish() if it is not available, or start the main / parent action.

Another way is to have your own default exception handler that restarts the main action, etc. after reporting an accident using ACRA (I believe there is a function request in ACRA bugtracker).

+3
source

I know this question is a bit outdated, but for future links and googlers, here is a complete answer from another, it’s hard to find, stackoverflow question: Android application restarts after failure / closing force


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 each thread (usually you have only one, unless you start a new thread (Async or ..., of course, you know about threads, if you can make a new one;)), set this class as DefaultUncaughtExceptionHandler

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

Remember this! Do this at the last stage of your application development, you should at least try to process all your exceptions one by one before leaving them for DefaultUncaughtExceptionHandler

+1
source

Source: https://habr.com/ru/post/901633/


All Articles