0

I created a custom application class to keep global informations of my app.

I need to get those informations back after my application restarts from a crash. I don't need to store those informations in disk because is just info from the user current session, but if the app crashed, I should start exactly where the user was before the crash.

I thought in two ways to solve my problem: 1-Track the variables changes and always persist it in the SharedPreferences 2-Always save then in the activity saveInstance and retain then from the savedInstanceBundle

The problem with the solution 1 is an overhead in every change. The problem with solution 2 is that I need to serialize every info.

Do you guys know any other way to solve that problem? I only need to store when the app crashs and load back after starting from a crash. Just in those two scenarios.

jonathanrz
  • 4,206
  • 6
  • 35
  • 58
  • why not just figure out why your app crashes and to something about it instead? – e4c5 Oct 03 '15 at 06:40
  • @e4c5 can you figure EVERY possible situation when your app crashes? Probably not, neither me, so a strategy to recover from any crash is always a good idea. – jonathanrz Oct 03 '15 at 11:21

1 Answers1

1

I'm currently using the following code to track whenever my app crashes, so i can show a better crash screen to the user, and it also allows me to send information about the crash to my server for later debugging.

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        //Get information and save the information 
    }
});

But just as a warning, you need to set this in every Activity / Service you create for it to be 100% effective.

Personally i have 1 base class for all my Activities, and 1 for all my Services, and then those classes implement the UncaughtExceptionHandler, so that can save ALOT of headeche and code copy-paste.

Moonbloom
  • 7,738
  • 3
  • 26
  • 38
  • If you need to restart your application after the crash, here is a good way: http://stackoverflow.com/questions/2681499/android-how-to-auto-restart-application-after-its-been-force-closed – jonathanrz Oct 03 '15 at 13:21