Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android App Restarts upon Crash/force close

My android app is getting restarted after force close, through my entire application which consist of 20 activities, I am relying on static data created on a main activity. So once the app is getting crashed all my static data is getting lost and when the app auto restarts practically it does not have any essential data to operate upon.

My question is, upon crash i want this things to happen

  1. If the app crashes, I don't want the app to restart rather I want all the stack/task related with this app to be wiped out of memory. A user can restart it from the beginning again
  2. If I can't prevent app from restart, at least I want to preserve essential data so that when the app restarts I can assign them back. Also when it restarts I want my app to start from the main activity.

I know when activity crashes android system will bring next activity in stack to foreground, and this is reason for my app producing redundant results. also i went through the android developers but only thing i got to know was setting up an attribute in Manifest android:finishOnTaskLaunch="true". But unfortunately this is of no help to me. I would appreciate your help on solving this issue, and also letting me know the cause and analysis.

like image 850
Techfist Avatar asked Sep 24 '12 07:09

Techfist


People also ask

Why do my apps keep force closing Android?

Apps on Android can crash because of low storage space, too many apps running simultaneously, a weak internet connection, or not having the proper app updates installed.

How do you auto restart an Android application after a crash or a force close error?

In order to restart your application when it crashed you should do the following : In the onCreate method, in your main activity initialize a PendingIntent member: Intent intent = PendingIntent. getActivity( YourApplication.

Why do my Android apps restart all over again if I minimize them how do I fix this?

What deep clean does is, whenever you minimize an app , it releases all the memory associated with that app , and hence when you again open that app, it will start as if it is new. To check the 'deep clean' option, go to the developer options of your phone. If it is not enabled please enable it.

How do I stop Android apps from force closing?

Force stop the app To do this, go to Settings -> Apps and select the app that keeps crashing. Tap on the app's name and then tap on 'Force stop'.


1 Answers

  1. Best solution would be instead of using Static data, use Shared Preferences or store data in a Database and if any uncaught Exception occurs, show a message like Application has crashed and a report is sent to the admin and then restart the Activity that caused the Crash. This way user can continue using the application.

  2. Do the same but instead of restarting the Activity which caused the Exception restart the application.

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);     } } 

and in every Activity create an Object of this class and set it as the DefaultUncaughtExceptionHandler

    Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler(this,             YourCurrentActivity.class)); 
like image 121
Archie.bpgc Avatar answered Nov 07 '22 14:11

Archie.bpgc