Is there a way to do some stuff before onCreate()
of the MainActivity is called?
We want to do some initializing stuff like logging...
According to the activity lifecycle, onCreate() is called once when the app is created, followed by the onStart() method which may be called multiple times throughout the activity lifecycle.
onCreate() - called before the first components of the application starts. onLowMemory() - called when the Android system requests that the application cleans up memory. onTrimMemory() - called when the Android system requests that the application cleans up memory.
OnStart is always called by the system after OnCreate is finished. Activities may override this method if they need to perform any specific tasks right before an activity becomes visible such as refreshing current values of views within the activity. Android will call OnResume immediately after this method.
onCreate() is called when the when the activity is first created. onStart() is called when the activity is becoming visible to the user.
Two options;
1 . If your logging is not related to the activity about to start, rather, you'd need 'some' initialisation before the first activity starts then subclass android.app.Application
. The onCreate
method here is pretty much the first thing to run when your application starts.
For example, in our app this is where we create our DI injector's or decide whether the app needs a database created ("Preparing for first use") type tasks.
This seems like a good fit for application wide logging subsystem initialisation...
2 . Failing that (and if you want to log precisely just before an Activity's onCreate
method is called) then this is a classic use-case for Aspects. We use AspectJ for similar reasons alongside database transaction management. Refer to this blog entry on how to weave the code you require in the Android build system.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With