How To Restart Android App After A Crash

Posted By : Rahul Baboria | 30-May-2018

Introduction:

 

The greatest bad dream of Android engineers is the Crash or Force Close Error that can happen when a client utilizes one of their applications. To be sure, it's dependably a negative message sent to the client and the significant hazard is that the client uninstalls the application. Tragically, you can't generally get appropriately all mistakes and some of the time you can't maintain a strategic distance from a Crash or a Force Close Error. In these particular cases, a great approach is to arrange auto restart for your Android Application. With this approach, you have better opportunities to keep clients on your application.

To begin, you have to make a custom Application class execution to get an occurrence of your setting ceaselessly :
 

public class MyApplication extends Application {
 public static MyApplication instance;
 @Override
 public void onCreate() {
   super.onCreate();
   instance = this;
 }
 @Override
 public Context getApplicationContext() {
   return super.getApplicationContext();
 }
 public static MyApplication getInstance() {
   return instance;
 }
}


 

Now, add application class implementation in android manifest.

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.android.myapp">
  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:clearTaskOnLaunch="true"
    tools:replace="android:allowBackup"    
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:name=".MyApplication">
    <activity android:name=".MyActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>
</manifest>

 

To attempt the auto restart highlight, we have to characterize a catch in the format of the Main Activity. When we will tap on the catch, we will crash the application. The format will have the accompanying structure :

 

public class UnCaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
    private Activity activity;

    public UnCaughtExceptionHandler(Activity a) {
        activity = a;
        Logr.d("Setting Uncaught Exception handler");
    }
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
         //do your life saving stuff here 
    }
}

At the point when an uncaught special case happens, the uncaughtException technique will be called. We will make an Intent to restart our application at the characterized minute through an Alarm. At that point, we complete the present movement and we leave the application. Note that we put a boolean parameter to demonstrate that the application is restarted. 

Last advance is to introduce the UncaughtExceptionHandler amid the beginning of the application by calling the static setDefaultUncaughtExceptionHandler strategy for the Thread class :

 

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        Thread.setDefaultUncaughtExceptionHandler(new UnCaughtExceptionHandler(this));
        init();
    }

 

Thanks.

 

 

 

 

About Author

Author Image
Rahul Baboria

Rahul Baboria is having good knowledge over Android Application.

Request for Proposal

Name is required

Comment is required

Sending message..