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.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ButterKnife.bind(this);
Thread.setDefaultUncaughtExceptionHandler(new UnCaughtExceptionHandler(this));
init();
}
Thanks.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Rahul Baboria
Rahul Baboria is having good knowledge over Android Application.