Question

I need to restart my application after the crash occurs. My code below is not working. Am I missing anything?

import android.content.Context;
import android.content.Intent;

public class MyApplication implements java.lang.Thread.UncaughtExceptionHandler {
    private final Context myContext;

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

    public void uncaughtException(Thread thread, Throwable exception) {
        Intent intent = new Intent(myContext, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        myContext.startActivity(intent);

        System.exit(10);
    }
}
Was it helpful?

Solution

I'm telling you how you can simulate a restart, but you really, really SHOULDN'T be doing that!

    Intent mStartActivity = new Intent(getActivity(), YourActivity.class);
    int mPendingIntentId = 123453456;
    PendingIntent mPendingIntent = PendingIntent.getActivity(getActivity(),
            mPendingIntentId, mStartActivity,
            PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager) getActivity().getSystemService(
            Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100,
            mPendingIntent);
    System.exit(0);

Again, you should find a cleaner solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top