2

My application needs restart itself after catching some bad exception. question is how to restart my application it self ?

Context::finish() can just remove activity in stack top, NOT able to bring new created main activity to top

RoFF
  • 529
  • 5
  • 27
  • possible duplicate of [Android: How to auto-restart application after it's been "force closed"?](http://stackoverflow.com/questions/2681499/android-how-to-auto-restart-application-after-its-been-force-closed) – c0rp Jul 11 '14 at 11:38
  • Everything that extends `RuntimeException`, shouldn't be caught, normally. Runtime exception means that there is programming-related bug, and application can't recover from that, since it can't modify itself. Some further reading on Exceptions [here](http://www.javapractices.com/topic/TopicAction.do?Id=129) and [here](http://www.math.uni-hamburg.de/doc/java/tutorial/essential/exceptions/runtime.html) – Marius Jul 11 '14 at 11:47

2 Answers2

2

If restarting your application basically means start your LAUNCHER activity then you can simply start a new intent for your first activity with following flags :

homeActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
Parvaz Bhaskar
  • 1,367
  • 9
  • 29
0

Killing an app due to a raised exception is easy: just fo not catch() it away.

Android will respond to an uncaught exception by killing your app.

if you do catch these bad exception, simply make sure you re-through them after processing:

class doSomething() {

  try { 
       do something risky
  } catch (AdError e)
       record error to log , or notify server

       throw e; // <------- and rethrow the exception
  }

}


But you wanted app restart, which also involves starting your app back again.

This is, frankly, a tricky thing, and most apps do not do it!

But if I had to figure out an app restart scheme it would probably work like this:

  • At app startup declare an alarm which periodically, say every X seconds, wakes up and check if app had crashed
  • Before graceful going down of app the alarm is turned off
  • After app crash of app the alarm (which was not turned off!) will wake up and detect app has crashed in prior run, and will respond by restarting it.


Finally - A simple crash detector:


class MyFirstActivity  {

     void onCreate() {
          other staff....

          SharedPreferences preferences = getSharedPreferences("CRASH_INDICATOR",      MODE_PRIVATE);
          SharedPreferences.Editor editor = preferences.edit();
          editor.putBoolean("HAD_CRASHED", true); // <------------ mark as crashed
          editor.commit();          
     }

}




class MyLastActivity  {

     void onStop() {   // <----------------- app is now gracefully closing
          other staff....

          SharedPreferences preferences = getSharedPreferences("CRASH_INDICATOR",MODE_PRIVATE);
          SharedPreferences.Editor editor = preferences.edit();
          editor.putBoolean("HAD_CRASHED", false); // <------------ mark as NOT crashed
          editor.commit();          
     }

}


class Alarmreceiver { // <----- check whether app had crashed in prior run and, if so, react
      onReceive() {
           SharedPreferences preferences = getSharedPreferences("CRASH_INDICATOR", MODE_PRIVATE);
           boolean hadCrashed = preferences.getBoolean("HAD_CRASHED", false);
           if (hadCrashed ) {
                handle....
           }
      }

}
Gilad Haimov
  • 5,767
  • 2
  • 26
  • 30