Restart App after crash programmatically - Android

12,153

Solution 1

this will do the job for you.

How to start the automatically stopped android service?

still i don't understand why it is supposed to crash.

UPDATE

you create an handler for uncaught exception

    private Thread.UncaughtExceptionHandler onRuntimeError= new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread thread, Throwable ex) {
            //Try starting the Activity again
    };

in your on create, you register an handler for uncaught exception

    @Override
    protected void onCreate() { 
        super.onCreate();
        Thread.setDefaultUncaughtExceptionHandler(onRuntimeError);  
    }

Solution 2

I might be late a lot, but I found 2 in 1 solution for your problem.

public void doRestart() {
    Intent mStartActivity = new Intent(context, LoginActivity.class);
    int mPendingIntentId = 123456;
    PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
    System.exit(0);
}

private void appInitialization() {
    defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
    Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
}

//make crash report on ex.stackreport
private Thread.UncaughtExceptionHandler defaultUEH;
// handler listener
private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        ex.printStackTrace();
        doRestart();
    }
};



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_index);
    appInitialization();
Share:
12,153
Sith
Author by

Sith

Updated on June 04, 2022

Comments

  • Sith
    Sith about 2 years

    Is there a way for my to program my app to automatically restart itself whenever it crashes? My app is just a simple media rendering App, however it occasionally crashes ( it's supposed to ). Is this at all possible? Thanks. My code looks like this

    public void Play(){  if(mp != null) {
                 mp.reset();
                 mp.release();
                 mp = null;
             }
    AudioRenderer mr = new AudioRenderer(); 
    mp = mr.AudioRenderer(filePath);
    }
    
    private class AudioRenderer extends Activity {
    private MediaPlayer AudioRenderer(String filePath) {    
    File location = new File(filePath);
    Uri path = Uri.fromFile(location);
    mp= MediaPlayer.create(this, path);
    }
    return mp
    }
    
  • Sith
    Sith almost 13 years
    Thanks! I'm sure this will come in handy
  • Dave Sims
    Dave Sims over 12 years
    This seems like a unreliable way to handle all uncaught errors, and would be a confusing experience for the user, who only sees the app suddenly navigate to the top activity again. Are there conditions that you wouldn't want to restart that activity? Are you sure you want to restart the home Activity on all uncaught errors? Is the app in an inconsistent state? What does this do to your Activity stack? Are you logging the exception? Agree with CrackerJack9: you should wrap the specific code that is crashing occasionally, log and fall through. Global handlers like this are generally a Bad Idea.
  • Samuel
    Samuel over 12 years
    @DaveSims, I sure agree with you, i did quote "why it is supposed to crash." still my response was specific to the problem raised to by the op. And for all the questions you raised, i assumed 'if' conditions inside 'onRuntimeError'. ;)
  • sandrstar
    sandrstar about 3 years
    Do you still recommend this way? Is it better to do the same somehow with workmanager?