Now you can Subscribe using RSS

Submit your Email

Saturday

How to 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 Android 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:

restart-android-app-after-crash

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


This class is the core of the App Restart Feature.We have created a custom implementation of the UncaughtExceptionHandler interface. This is a relatively unknown Interface for handlers, it is invoked when a Thread abruptly terminates due to an exception that has not been caught by the program. So whenever the program has an exception for which you have not written the catch part, this interface will be called, so you can do whatever you want to do here.


3. Using in your Activity : (Put this code in onCreate of the Launcher Activity)
Thread.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.

0 comments:

Post a Comment

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