I have a requirement to provide option to user to relaunch the application whenever application gets force closed without going to Home.
Can it be possible??
I want to make sure is it possible or not??
Actually its a client requirement.
I have a requirement to provide option to user to relaunch the application whenever application gets force closed without going to Home.
Can it be possible??
I want to make sure is it possible or not??
Actually its a client requirement.
Yes this is possible.
In your application class call the set the following in onCreate
.
Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
Intent intent = getPackageManager().getLaunchIntentForPackage("Your packagename");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
};
This method will catch all the uncaught exceptions thrown by your code and restart your app without any crash.
Yes, It is possible you need to follow below 2 steps only..
Step 1 :- Create a class as MyCrashHandler
as
public static class MyCrashHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
//Handle your crash here...Show dialog etc..
ex.printStackTrace();
}
}
Step 2 :- Now you need to register this crash handler in your activity,,As
Thread.currentThread().setDefaultUncaughtExceptionHandler(new MyCrashHandler ());