1

I used to handle exception errors with this class and pass error as String to another activity for show , but this way not work any more , seems when kill process happened "sag.class" Activity cant launch any more

public class ExceptionHandler extends MainActivity implements java.lang.Thread.UncaughtExceptionHandler {

    private final Context myContext;

    private SharedPreferences prefs;

    private StringBuilder errorReport;

    public ExceptionHandler(Context context) {
        myContext = context;
    }

    public void uncaughtException(Thread thread, Throwable exception) {



        StringWriter stackTrace = new StringWriter();
        exception.printStackTrace(new PrintWriter(stackTrace));
        System.err.println(stackTrace);

        exception.printStackTrace(new PrintWriter(stackTrace));
        errorReport = new StringBuilder();
        errorReport.append("************ CAUSE OF ERROR ************\n\n");
        errorReport.append(stackTrace.toString());

        errorReport.append("\n************ DEVICE INFORMATION ***********\n");

        errorReport.append(LINE_SEPARATOR);
        errorReport.append("Product: ");
        errorReport.append(Build.PRODUCT);
        errorReport.append(LINE_SEPARATOR);
        errorReport.append("\n************ FIRMWARE ************\n");
        errorReport.append("SDK: ");
        errorReport.append(Build.VERSION.SDK);
        errorReport.append(LINE_SEPARATOR);
        errorReport.append("Release: ");
        errorReport.append(Build.VERSION.RELEASE);
        errorReport.append(LINE_SEPARATOR);
        errorReport.append("Incremental: ");
        errorReport.append(Build.VERSION.INCREMENTAL);
        errorReport.append(LINE_SEPARATOR);


        errorReport.append("App Version: ");
        try {
            errorReport.append(myContext.getApplicationContext().getPackageManager().getPackageInfo(myContext.getPackageName(), 0).versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        errorReport.append(LINE_SEPARATOR);


        prefs = myContext.getSharedPreferences("MyPrefsFile", MODE_PRIVATE);

        if (prefs.contains("access_token"))
        {
            errorReport.append(prefs.getString("fname","")+" "+prefs.getString("lname",""));
        }


        Intent intent = new Intent(myContext, sag.class);
        intent.putExtra("error", errorReport.toString());
        myContext.startActivity(intent);

        Process.killProcess(Process.myPid());
        System.exit(10);

    }


}

so how can start activity in UncaughtExceptionHandler class with new way ?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Erfan
  • 3,059
  • 3
  • 22
  • 49

2 Answers2

0

try this, please add this code to start new activity

Intent startAppIntent = new Intent(this, sag.class);
        startAppIntent.putExtra("error", errorReport.toString());
        startAppIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startAppIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startAppIntent);
        System.exit(1);

you can change this to myContext if this doesn't work. Please let us know if this code works for you.

Amol Gangadhare
  • 1,059
  • 2
  • 11
  • 24
  • 1
    @erfan I get the same strange behaviour. The Intent is not firing. Did you find solution on this problem? If yes, could you please share it? Thanks in advance. – BWappsAndmore Jun 29 '20 at 08:19
0

Short Answer : Yes you can start an activity from UncaughtExceptionHandler.

fun restartApp(context : Context) {
    val intent = Intent(context, Destination::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    context.startActivity(intent)
    android.os.Process.killProcess(android.os.Process.myPid())
    exitProcess(0)
}

you can also find the exit status number in this link

BWappsAndmore
  • 491
  • 3
  • 17
Yoshimitsu
  • 4,343
  • 2
  • 22
  • 28