0

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.

Neha Shukla
  • 3,572
  • 5
  • 38
  • 69
  • possible duplicate of [Android App Restarts upon Crash/force close](http://stackoverflow.com/questions/12560590/android-app-restarts-upon-crash-force-close) – Piyush Jun 09 '15 at 09:34

2 Answers2

3

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.

Nitesh
  • 3,868
  • 1
  • 20
  • 26
  • OP is not talking about a crash, but a forced close of the activity. Does the forced close raise an exception? – dhke Jun 09 '15 at 09:34
0

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 ());
IshRoid
  • 3,696
  • 2
  • 26
  • 39