Now you can Subscribe using RSS

Submit your Email


Auto restart an app after it crashes | Android

Vishal Shrestha
Sometimes you may have forgotten to keep all the possible scenarios in mind and might have missed to handle some possible exceptions, and your app crashes with a nasty exception. In case you were doing some task that you would like to resume and restart app without user intervention, you can do it in the following way easily:


How to auto restart Android app after crash

  1. Create a class that extends the Application class of android. Application class helps in maintaining global application state, you can learn more about it here : Application Class.
  2. Create a class extending UncaughtExceptionHandler of the thread class in java. When you app crashes without proper exception handling, uncaughtException method in this class will be called. We will restart the app in this method.
  3. Simply set this class that we just made as the default uncaught exception handler and voila! Your app is restarted when it crashes.
Here's the sample code to restart android app after is crashes : 

1. The ApplicationClass :
public class ApplicationClass extends Application {
    private static Context mContext;
    public static ApplicationClass instace;
    @Override    public void onCreate() {
        super.onCreate();        mContext = getApplicationContext();        instace = this;    }

    @Override    public Context getApplicationContext() {
        return super.getApplicationContext();    }

    public static ApplicationClass getInstance() {
        return instace;    }

2. The DefaultExceptionHandler class :
public class DefaultExceptionHandler implements Thread.UncaughtExceptionHandler {
    Activity activity;
    public DefaultExceptionHandler(Activity activity) {
        this.activity = activity;    }

    @Override    public void uncaughtException(Thread thread, Throwable ex) {

            Intent intent = new Intent(activity, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP                    | Intent.FLAG_ACTIVITY_CLEAR_TASK                    | Intent.FLAG_ACTIVITY_NEW_TASK);
            PendingIntent pendingIntent = PendingIntent.getActivity(
                    ApplicationClass.getInstance().getBaseContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
            //Restart your app after 2 seconds            AlarmManager mgr = (AlarmManager) ApplicationClass.getInstance().getBaseContext()
                    .getSystemService(Context.ALARM_SERVICE);            mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000,                    pendingIntent);
            //finishing the activity.            activity.finish();            //Stopping application            System.exit(2);        }

3. Using in your Activity : (Put this code in onCreate of the Activity which can crash)
//To Restart if exits with exceptionThread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler(this));

4. Add android:name is manifest.xml : 
<application    android:allowBackup="true"    android:icon="@drawable/ic"    android:name="vysh.sample.ApplicationClass"    android:label="@string/app_name"    android:supportsRtl="true"    android:theme="@style/AppTheme">

This is all it takes to restart you app after it has crashed! If you have any problems, feel free to drop a comment below or message me! Cheers!

Vishal Shrestha / Author & Founder

A developer by profession, a born Adventurer. I mainly do Android but like to get my hands dirty with web development and a little bit of Python. I would't rather go on a Trek than a party and you can find me having a few rounds with the heavy bag to let out the steam ;)

For Business info : My Portfolio Site.


Post a Comment

Coprights @ 2017 | The Code City by Vishal Shrestha Vishal Shrestha