Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do something before onCreate()?

Tags:

android

Is there a way to do some stuff before onCreate() of the MainActivity is called?

We want to do some initializing stuff like logging...

like image 591
Jack Avatar asked Jun 07 '13 09:06

Jack


People also ask

Is onStart before onCreate?

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.

Which Android method is before onCreate?

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.

What comes after onCreate?

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.

What is used of onCreate () method?

onCreate() is called when the when the activity is first created. onStart() is called when the activity is becoming visible to the user.


1 Answers

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.

like image 87
BrantApps Avatar answered Oct 21 '22 06:10

BrantApps