0

I want to use Crashlytics. I initialize Crashlytics in the onCreate function from my main activity and everything works well.

The problem starts if the app is not active for a while and I want to jump into the app from a status bar notification. I think it gets recycled from the Android garbage collection. So I have to reinitialize Crashlytics. It's possible that I get from the status bar a notification to nearly every activity in my app (I also trigger async task from the status bar).

Do I have to initialize Crashlytics in every activity/async task to make sure that it's working well after the app gets closed and recycled or is there another method?

honk
  • 9,137
  • 11
  • 75
  • 83
Peter
  • 9
  • 2

1 Answers1

5

You should initialise Crashlytics in your Application class. As long as your app is kept alive by the system, an instance of Application will be available.

You can create a custom application class using something similar to below:

public class MyCustomApplication extends Application {
    public void onCreate() {
        super.onCreate();
        Fabric.with(this, new Crashlytics());
    }
}
fractalwrench
  • 4,028
  • 7
  • 33
  • 49
  • Thanks for the quick response. Looks very useful! Just for understanding: the onCreate methode in my custom application class will also be executed if i just execute an async task without an activity? – Peter Dec 22 '15 at 20:16
  • Try to avoid putting too much code into the application class, as it can get slow down app startup. But yes, as long as you don't remove the super call it should work. – fractalwrench Dec 22 '15 at 20:21